package uk.ac.ic.wlgitbridge.bridge.swap.store;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;

import java.io.InputStream;

/**
 * Created by winston on 21/08/2016.
 */
public class S3SwapStore implements SwapStore {

    private final AmazonS3 s3;

    private final String bucketName;

    public S3SwapStore(SwapStoreConfig cfg) {
        this(
                cfg.getAwsAccessKey(),
                cfg.getAwsSecret(),
                cfg.getS3BucketName()
        );
    }

    S3SwapStore(
            String accessKey,
            String secret,
            String bucketName
    ) {
        s3 = new AmazonS3Client(new BasicAWSCredentials(accessKey, secret));
        this.bucketName = bucketName;
    }

    @Override
    public void upload(
            String projectName,
            InputStream uploadStream,
            long contentLength
    ) {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(contentLength);
        PutObjectRequest put = new PutObjectRequest(
                bucketName,
                projectName,
                uploadStream,
                metadata
        );
        PutObjectResult res = s3.putObject(put);
    }

    @Override
    public InputStream openDownloadStream(String projectName) {
        GetObjectRequest get = new GetObjectRequest(
                bucketName,
                projectName
        );
        S3Object res = s3.getObject(get);
        return res.getObjectContent();
    }

    @Override
    public void remove(String projectName) {
        DeleteObjectRequest del = new DeleteObjectRequest(
                bucketName,
                projectName
        );
        s3.deleteObject(del);
    }

}

@@ -42,16 +42,72 @@
 store;%0A%0A
+import com.amazonaws.auth.AWSStaticCredentialsProvider;%0A
 import c
@@ -235,16 +235,23 @@
 S3Client
+Builder
 ;%0Aimport
@@ -795,20 +795,16 @@
     s3 =
- new
  AmazonS
@@ -810,16 +810,83 @@
 S3Client
+Builder.standard().withCredentials(new AWSStaticCredentialsProvider
 (new Bas
@@ -921,16 +921,25 @@
 secret))
+).build()
 ;%0A      

Drop use of deprecated interface