package com.conveyal.r5.multipoint;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.conveyal.r5.analyst.cluster.AnalysisTask;
import org.joda.time.DateTime;

import java.io.*;
import java.util.zip.GZIPOutputStream;

/**
 * Store static site publication data in S3.
 */
public abstract class MultipointDataStore {
    private static AmazonS3 s3 = new AmazonS3Client();

    /**
     * Get an output stream to upload an object to S3 for the given static site request.
     * There is no need to gzip data going into this stream, it will be gzipped on upload in storage and when downloaded
     */
    public static OutputStream getOutputStream (AnalysisTask req, String filename, String type) throws IOException {
        ObjectMetadata md = new ObjectMetadata();
        // This way the browser will decompress on download
        // http://www.rightbrainnetworks.com/blog/serving-compressed-gzipped-static-files-from-amazon-s3-or-cloudfront/
        md.setContentEncoding("gzip");
        md.setContentType(type);

        PipedOutputStream pipeOut = new PipedOutputStream();
        PipedInputStream pipeIn = new PipedInputStream(pipeOut);

        //TODO use a better key
        String key = "static-results/" + req.outputDirectory + "/" + filename;

        PutObjectRequest por = new PutObjectRequest(req.outputBucket, key, pipeIn, md);
        // buffer files up to 100MB
        por.getRequestClientOptions().setReadLimit(100 * 1024 * 1024 + 1);

        // run s3 put in background thread so pipedinputstream does not block.
        // TODO gzip in background as well?
        new Thread(() -> {
            s3.putObject(por);
        }).start();

        return new GZIPOutputStream(pipeOut);
    }
}
@@ -526,24 +526,109 @@
 3Client();%0A%0A
+//    static %7B%0A//        s3.setRegion(Region.getRegion(Regions.EU_WEST_1));%0A//    %7D%0A%0A
     /**%0A    
@@ -1392,120 +1392,8 @@
 );%0A%0A
-        //TODO use a better key%0A        String key = %22static-results/%22 + req.outputDirectory + %22/%22 + filename;%0A%0A
     
@@ -1444,29 +1444,48 @@
 est(
-req.outputBucket, key
+%22analysis-static/%22 + req.jobId, filename
 , pi

store static site data in simpler path on s3