package com.clinichelper.Service;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

/**
 * Created by Eduardo veras on 11-Nov-16.
 */
@Service
public class AmazonService {

    private static String bucketName     = "*** Provide bucket name ***";
    private static String keyName        = "*** Provide key ***";
    private static String uploadFileName = "*** Provide file name ***";

    public void UploadImageToAWS() {
        AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
        try {
            System.out.println("Uploading a new object to S3 from a file\n");
            File file = new File(uploadFileName);
            s3client.putObject(new PutObjectRequest(
                    bucketName, keyName, file));

        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which " +
                    "means your request made it " +
                    "to Amazon S3, but was rejected with an error response" +
                    " for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which " +
                    "means the client encountered " +
                    "an internal error while trying to " +
                    "communicate with S3, " +
                    "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        }
    }
}

@@ -562,37 +562,40 @@
   = 
-%22*** Provide bucket name ***%22
+System.getenv(%22AWS_BUCKET_NAME%22)
 ;%0A  
@@ -639,140 +639,158 @@
   = 
-%22*** Provide key ***%22;%0A    private static String uploadFileName = %22*** Provide file name ***%22;%0A%0A    public void UploadImageToAWS() %7B
+System.getenv(%22AWS_BUCKET_KEY%22);%0A%0A    public void UploadImageToAWS(String uploadFileName,File file) %7B%0A        file.renameTo(new File(uploadFileName));
 %0A   
@@ -964,57 +964,8 @@
 %22);%0A
-            File file = new File(uploadFileName);
 %0A   

update aws