package com.bizbeam.s3.uploader;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.PutObjectRequest;
import org.apache.log4j.Logger;

import java.io.File;

public class S3Connector {

    private String mAWS_S3_BUCKET;
    private String mAWS_ACCESS_KEY;
    private String mAWS_SECRET_KEY;
    private AmazonS3 mAmazonS3;
    private boolean mInitialized = false;

    public S3Connector(String s3Bucket, String s3AccessKey, String s3SecretKey) {
        mAWS_S3_BUCKET = s3Bucket;
        mAWS_ACCESS_KEY = s3AccessKey;
        mAWS_SECRET_KEY = s3SecretKey;
    }

    public void initialize() {
        if ((mAWS_ACCESS_KEY != null) && (mAWS_SECRET_KEY != null)) {
            AWSCredentials awsCredentials = new BasicAWSCredentials(mAWS_ACCESS_KEY, mAWS_SECRET_KEY);
            mAmazonS3 = new AmazonS3Client(awsCredentials);

            LOGGER.info("Using S3 Bucket: " + mAWS_S3_BUCKET);
            mInitialized = true;
        }
    }

    public void uploadFile(File file) throws Exception{
        if (!mInitialized)
            throw new Exception(CONNECTOR_NOT_INITIAZLIED_ERROR_MSG);

        PutObjectRequest putObjectRequest = new PutObjectRequest(mAWS_S3_BUCKET, file.getName(), file);
        putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead);
        mAmazonS3.putObject(putObjectRequest);
    }

    public void createNewBucket(String bucketName) throws Exception {
        if (!mInitialized)
            throw new Exception(CONNECTOR_NOT_INITIAZLIED_ERROR_MSG);

        mAmazonS3.createBucket(bucketName);
    }

    public void removeFile(String objectKey) throws Exception {
        if (!mInitialized)
            throw new Exception(CONNECTOR_NOT_INITIAZLIED_ERROR_MSG);

        mAmazonS3.deleteObject(mAWS_S3_BUCKET, objectKey);
    }


    private static String CONNECTOR_NOT_INITIAZLIED_ERROR_MSG = "Connector not initialized";
    private static final Logger LOGGER = Logger.getLogger(S3Connector.class);
}

@@ -208,16 +208,74 @@
 Client;%0A
+import com.amazonaws.services.s3.model.AmazonS3Exception;%0A
 import c
@@ -326,24 +326,24 @@
 ontrolList;%0A
-
 import com.a
@@ -882,16 +882,34 @@
 ialize()
+  throws Exception
  %7B%0A     
@@ -1133,24 +1133,287 @@
 dentials);%0A%0A
+            try %7B%0A                mAmazonS3.listBuckets();%0A            %7D catch (AmazonS3Exception exc) %7B%0A                if (exc.getErrorCode().equals(%22SignatureDoesNotMatch%22))%0A                    throw new Exception(%22WRONG AWS KEY OR PASSWORD!%22);%0A            %7D%0A%0A
             
@@ -1505,32 +1505,32 @@
        %7D%0A    %7D%0A%0A
-
     public void 
@@ -1567,16 +1567,17 @@
 xception
+ 
 %7B%0A      

verify correct log in process