Example usage for com.amazonaws.services.s3 AmazonS3 putObject

List of usage examples for com.amazonaws.services.s3 AmazonS3 putObject

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3 putObject.

Prototype

public PutObjectResult putObject(PutObjectRequest putObjectRequest)
        throws SdkClientException, AmazonServiceException;

Source Link

Document

Uploads a new object to the specified Amazon S3 bucket.

Usage

From source file:S3Sample.java

License:Open Source License

public static void main(String[] args) throws IOException {
    // s3/*from   w  w  w . j a va2s  . com*/
    String imageBucketName = "ringtone_image";
    String ringBucketName = "ringtone_ring";
    AmazonS3 s3 = new AmazonS3Client(
            new PropertiesCredentials(S3Sample.class.getResourceAsStream("AwsCredentials.properties")));
    String bucketName;
    System.out.println("Start Sending Files To Amazon S3");

    // directory and files
    String pathName = "/home/liutao/workspace/python/1-fetch/download_Holiday/";
    File dir = new File(pathName);
    File list[] = dir.listFiles();
    String fileName;

    // sort by record index
    Arrays.sort(list, new Comparator<File>() {
        public int compare(File f1, File f2) {
            String s1 = f1.getName();
            String s2 = f2.getName();
            int idx1 = s1.indexOf(".xml");
            int idx2 = s2.indexOf(".xml");
            if (idx1 != -1 && idx2 != -1) {
                int num1 = Integer.parseInt(s1.substring(s1.indexOf('d') + 1, idx1));
                int num2 = Integer.parseInt(s2.substring(s2.indexOf('d') + 1, idx2));
                return num1 - num2;
            } else if (idx1 == -1)
                return -1;
            else
                return 1;
        }
    });

    // xml parser
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    InputStream in;

    // 
    String uuid;
    String key;
    URL url;
    int i = 0, j;

    try {
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        FileWriter log = new FileWriter(pathName + "log");

        while (i < list.length) {
            if (list[i].getName().endsWith(".xml"))
                break;
            i++;
        }
        // start process xml file
        for (; i < list.length; i++) {
            System.out.println(list[i].getName());
            log.write(list[i].getName() + ",");
            try {
                in = new FileInputStream(pathName + list[i].getName());
                Document doc = docBuilder.parse(in);
                org.w3c.dom.Element root = doc.getDocumentElement();
                //System.out.println(root.getNodeName());
                NodeList childen = root.getChildNodes();
                org.w3c.dom.Node curNode, node;

                uuid = UUID.randomUUID().toString();

                for (j = 0; j < childen.getLength(); j++) {
                    curNode = childen.item(j);
                    if (curNode.getNodeType() == Node.ELEMENT_NODE)
                        if (curNode.getNodeName().equals("Ring") || curNode.getNodeName().equals("Image")) {
                            //System.out.println(curNode.getFirstChild().getNodeValue());
                            fileName = curNode.getFirstChild().getNodeValue();
                            File file = new File(pathName + fileName);
                            if (!file.exists()) {
                                log.write(fileName + " not Found!\n");
                                break;
                            }

                            key = uuid + fileName;
                            //bucketName = curNode.getNodeName().equals("Ring")?ringBucketName:imageBucketName;
                            bucketName = "ringtone_test_2010";
                            try {
                                s3.putObject(new PutObjectRequest(bucketName, key, file)); //        
                                s3.setObjectAcl(bucketName, key, CannedAccessControlList.PublicRead); // ??
                                // url = s3.generatePresignedUrl(bucketName, key, expireDate);
                                // System.out.println(url);
                            } catch (AmazonServiceException ase) {
                                log.write("AmazonServiceException\n");
                            } catch (AmazonClientException ace) {
                                log.write("AmazonClientException\n");
                            }
                        }
                }
                if (j == childen.getLength()) {// if success, record it
                    log.write("uuid:" + uuid + "\n");
                }
            } catch (FileNotFoundException e) {
                log.write("FileNotFound\n");
            } catch (SAXException e) {
                log.write("xml parse error\n");
            } catch (IOException e) {
                log.write("IOexception\n");
            }

        }

        log.flush();
        log.close();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    System.out.println("Sending Finished!");

}

From source file:BackupManager.java

License:Open Source License

public static void main(String[] args) throws IOException {

    AmazonS3 s3 = new AmazonS3Client(
            new PropertiesCredentials(BackupManager.class.getResourceAsStream("AwsCredentials.properties")));

    String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
    String key = "MyObjectKey";

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon S3");
    System.out.println("===========================================\n");

    try {//from   w w w.  j  av  a 2  s .com
        /*
         * Create a new S3 bucket - Amazon S3 bucket names are globally unique,
         * so once a bucket name has been taken by any user, you can't create
         * another bucket with that same name.
         *
         * You can optionally specify a location for your bucket if you want to
         * keep your data closer to your applications or users.
         */
        System.out.println("Creating bucket " + bucketName + "\n");
        s3.createBucket(bucketName);

        /*
         * List the buckets in your account
         */
        System.out.println("Listing buckets");
        for (Bucket bucket : s3.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
        System.out.println();

        /*
         * Upload an object to your bucket - You can easily upload a file to
         * S3, or upload directly an InputStream if you know the length of
         * the data in the stream. You can also specify your own metadata
         * when uploading to S3, which allows you set a variety of options
         * like content-type and content-encoding, plus additional metadata
         * specific to your applications.
         */
        System.out.println("Uploading a new object to S3 from a file\n");
        s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));

        /*
         * Download an object - When you download an object, you get all of
         * the object's metadata and a stream from which to read the contents.
         * It's important to read the contents of the stream as quickly as
         * possibly since the data is streamed directly from Amazon S3 and your
         * network connection will remain open until you read all the data or
         * close the input stream.
         *
         * GetObjectRequest also supports several other options, including
         * conditional downloading of objects based on modification times,
         * ETags, and selectively downloading a range of an object.
         */
        System.out.println("Downloading an object");
        S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
        System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
        displayTextInputStream(object.getObjectContent());

        /*
         * List objects in your bucket by prefix - There are many options for
         * listing the objects in your bucket.  Keep in mind that buckets with
         * many objects might truncate their results when listing their objects,
         * so be sure to check if the returned object listing is truncated, and
         * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve
         * additional results.
         */
        System.out.println("Listing objects");
        ObjectListing objectListing = s3
                .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My"));
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(
                    " - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
        }
        System.out.println();

        /*
         * Delete an object - Unless versioning has been turned on for your bucket,
         * there is no way to undelete an object, so use caution when deleting objects.
         */
        System.out.println("Deleting an object\n");
        s3.deleteObject(bucketName, key);

        /*
         * Delete a bucket - A bucket must be completely empty before it can be
         * deleted, so remember to delete any objects from your buckets before
         * you try to delete them.
         */
        System.out.println("Deleting bucket " + bucketName + "\n");
        s3.deleteBucket(bucketName);
    } 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 "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:PutTestFile.java

License:Apache License

public static void main(String[] args) throws IOException {
    /*/* w  w w. ja  va2  s.c  o  m*/
     * Important: Be sure to fill in your AWS access credentials in the
     * AwsCredentials.properties file before you try to run this sample.
     * http://aws.amazon.com/security-credentials
     */
    AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(

            PutTestFile.class.getResourceAsStream("AwsCredentials.properties")));
    String bucketName = "dtccTest";

    String key = "largerfile.txt";

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon S3");
    System.out.println("===========================================\n");

    try {

        /*
         * List the buckets in your account
         */
        System.out.println("Listing buckets");
        for (Bucket bucket : s3.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
        System.out.println();

        System.out.println("Uploading a new object to S3 from a file\n");

        PutObjectRequest request = new PutObjectRequest(bucketName, key, createSampleFile());
        request.setCannedAcl(CannedAccessControlList.PublicRead);
        s3.putObject(request);

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, whichmeans your request made it "
                + "to Amazon S3, but was rejected with an errorresponse 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, whichmeans the client encountered "
                + "a serious internal problem while trying tocommunicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:S3Sample.java

License:Open Source License

public static void main(String[] args) throws IOException {
    /*/*w w w  .jav a 2  s .c  o  m*/
     * Important: Be sure to fill in your AWS access credentials in the
     *            AwsCredentials.properties file before you try to run this
     *            sample.
     * http://aws.amazon.com/security-credentials
     */
    AmazonS3 s3 = new AmazonS3Client(
            new PropertiesCredentials(S3Sample.class.getResourceAsStream("AwsCredentials.properties")));

    String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
    String key = "MyObjectKey";

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon S3");
    System.out.println("===========================================\n");

    try {
        /*
         * Create a new S3 bucket - Amazon S3 bucket names are globally unique,
         * so once a bucket name has been taken by any user, you can't create
         * another bucket with that same name.
         *
         * You can optionally specify a location for your bucket if you want to
         * keep your data closer to your applications or users.
         */
        System.out.println("Creating bucket " + bucketName + "\n");
        s3.createBucket(bucketName);

        /*
         * List the buckets in your account
         */
        System.out.println("Listing buckets");
        for (Bucket bucket : s3.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
        System.out.println();

        /*
         * Upload an object to your bucket - You can easily upload a file to
         * S3, or upload directly an InputStream if you know the length of
         * the data in the stream. You can also specify your own metadata
         * when uploading to S3, which allows you set a variety of options
         * like content-type and content-encoding, plus additional metadata
         * specific to your applications.
         */
        System.out.println("Uploading a new object to S3 from a file\n");
        s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));

        /*
         * Download an object - When you download an object, you get all of
         * the object's metadata and a stream from which to read the contents.
         * It's important to read the contents of the stream as quickly as
         * possibly since the data is streamed directly from Amazon S3 and your
         * network connection will remain open until you read all the data or
         * close the input stream.
         *
         * GetObjectRequest also supports several other options, including
         * conditional downloading of objects based on modification times,
         * ETags, and selectively downloading a range of an object.
         */
        System.out.println("Downloading an object");
        S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
        System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
        displayTextInputStream(object.getObjectContent());

        /*
         * List objects in your bucket by prefix - There are many options for
         * listing the objects in your bucket.  Keep in mind that buckets with
         * many objects might truncate their results when listing their objects,
         * so be sure to check if the returned object listing is truncated, and
         * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve
         * additional results.
         */
        System.out.println("Listing objects");
        ObjectListing objectListing = s3
                .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My"));
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(
                    " - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
        }
        System.out.println();

        /*
         * Delete an object - Unless versioning has been turned on for your bucket,
         * there is no way to undelete an object, so use caution when deleting objects.
         */
        System.out.println("Deleting an object\n");
        s3.deleteObject(bucketName, key);

        /*
         * Delete a bucket - A bucket must be completely empty before it can be
         * deleted, so remember to delete any objects from your buckets before
         * you try to delete them.
         */
        System.out.println("Deleting bucket " + bucketName + "\n");
        s3.deleteBucket(bucketName);
    } 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 "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:PutFile.java

License:Apache License

public static void main(String[] args) throws IOException {
    /*/*ww w.j  a va 2s  . c o  m*/
     * Important: Be sure to fill in your AWS access credentials in the
     * AwsCredentials.properties file before you try to run this sample.
     * http://aws.amazon.com/security-credentials
     */
    AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(

            PutFile.class.getResourceAsStream("AwsCredentials.properties")));
    String bucketName = "dtccTest";

    System.out.println("===========================================");
    System.out.println(" Amazon S3 Test");
    System.out.println("===========================================\n");

    try {

        /*
         * List the buckets in your account
         */
        System.out.println("Listing buckets");
        for (Bucket bucket : s3.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
        System.out.println();
        String fileName = "S3HWTest.zip";
        File file = new File(fileName);
        System.out.println("Uploading a new object to S3 from a file " + fileName + "\n");
        System.out.println("file.length() " + file.length() + "\n");

        long start = System.currentTimeMillis();
        PutObjectRequest request = new PutObjectRequest(bucketName, fileName, file);
        request.setCannedAcl(CannedAccessControlList.PublicRead);
        s3.putObject(request);
        System.out.println("Uploading done in " + (System.currentTimeMillis() - start) + " ms.\n");

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, whichmeans your request made it "
                + "to Amazon S3, but was rejected with an errorresponse 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, whichmeans the client encountered "
                + "a serious internal problem while trying tocommunicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:Uploader.java

License:Open Source License

private static void uploadFile(AmazonS3 s3, String bucketName, String key, File file) {
    if (!file.exists()) {
        System.out.println("File does not exist: " + file.getAbsolutePath());
        return;// w w  w.j a  v  a  2 s.  c o  m
    }
    /*
     * Upload an object to your bucket - You can easily upload a file to S3,
     * or upload directly an InputStream if you know the length of the data
     * in the stream. You can also specify your own metadata when uploading
     * to S3, which allows you set a variety of options like content-type
     * and content-encoding, plus additional metadata specific to your
     * applications.
     */
    try {
        System.out.println(file.getAbsolutePath() + " ---> " + key + "\n");
        s3.putObject(new PutObjectRequest(bucketName, key, file));
        // Change permissions. Grant all users the read permission.
        AccessControlList acl = s3.getObjectAcl(bucketName, key);
        Permission permission = Permission.Read;
        Grantee grantee = GroupGrantee.AllUsers;
        acl.grantPermission(grantee, permission);
        s3.setObjectAcl(bucketName, key, acl);
    } 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 "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }

}

From source file:aws.sample.S3Sample.java

License:Open Source License

public static void main(String[] args) throws IOException {
    /*/* w w  w . j av a  2  s .  c  o m*/
     * Important: Be sure to fill in your AWS access credentials in the AwsCredentials.properties file before you try to run this sample. http://aws.amazon.com/security-credentials
     */
    AmazonS3 s3 = new AmazonS3Client(
            new PropertiesCredentials(S3Sample.class.getResourceAsStream("/AwsCredentials.properties")));

    String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
    String key = "MyObjectKey";

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon S3");
    System.out.println("===========================================\n");

    try {
        /*
         * Create a new S3 bucket - Amazon S3 bucket names are globally unique, so once a bucket name has been taken by any user, you can't create another bucket with that same name.
         * 
         * You can optionally specify a location for your bucket if you want to keep your data closer to your applications or users.
         */
        System.out.println("Creating bucket " + bucketName + "\n");
        s3.createBucket(bucketName);

        /*
         * List the buckets in your account
         */
        System.out.println("Listing buckets");
        for (Bucket bucket : s3.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
        System.out.println();

        /*
         * Upload an object to your bucket - You can easily upload a file to S3, or upload directly an InputStream if you know the length of the data in the stream. You can also specify your own metadata when uploading to S3, which allows you set a variety of options like content-type and content-encoding, plus additional metadata specific to your applications.
         */
        System.out.println("Uploading a new object to S3 from a file\n");
        s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));

        /*
         * Download an object - When you download an object, you get all of the object's metadata and a stream from which to read the contents. It's important to read the contents of the stream as quickly as possibly since the data is streamed directly from Amazon S3 and your network connection will remain open until you read all the data or close the input stream.
         * 
         * GetObjectRequest also supports several other options, including conditional downloading of objects based on modification times, ETags, and selectively downloading a range of an object.
         */
        System.out.println("Downloading an object");
        S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
        System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
        displayTextInputStream(object.getObjectContent());

        /*
         * List objects in your bucket by prefix - There are many options for listing the objects in your bucket. Keep in mind that buckets with many objects might truncate their results when listing their objects, so be sure to check if the returned object listing is truncated, and use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve additional results.
         */
        System.out.println("Listing objects");
        ObjectListing objectListing = s3
                .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My"));
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(
                    " - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
        }
        System.out.println();

        /*
         * Delete an object - Unless versioning has been turned on for your bucket, there is no way to undelete an object, so use caution when deleting objects.
         */
        System.out.println("Deleting an object\n");
        s3.deleteObject(bucketName, key);

        /*
         * Delete a bucket - A bucket must be completely empty before it can be deleted, so remember to delete any objects from your buckets before you try to delete them.
         */
        System.out.println("Deleting bucket " + bucketName + "\n");
        s3.deleteBucket(bucketName);
    } 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 "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:awslabs.lab21.SolutionCode.java

License:Open Source License

@Override
public void putObject(AmazonS3 s3Client, String bucketName, String sourceFileName, String objectKey) {
    File sourceFile = new File(sourceFileName);

    // Construct a PutObjectRequest object using the values provided in the method parameters 
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectKey, sourceFile);

    // Upload the object by submitting the request using the putObject method of the s3Client object.
    s3Client.putObject(putObjectRequest);
}

From source file:beanstalk.BeanstalkDeployNoGUI.java

License:Apache License

public void upload_file_to_s3(String bucketName, String keyName, String uploadFileName, String AWSKeyId,
        String AWSSecretKey) throws BeanstalkAdapterException {

    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);

    AmazonS3 s3client = new AmazonS3Client(basic_credentials);

    try {//w  ww  .j a  va  2 s. c  o  m
        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());
        throw new BeanstalkAdapterException(ase.getMessage());

    } 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());

        throw new BeanstalkAdapterException(ace.getMessage());
    }

}

From source file:beanstalk.BeanstalkFirstDeploymentNoGUI.java

License:Apache License

public void upload_file_to_s3(String bucketName, String keyName, String uploadFileName, String AWSKeyId,
        String AWSSecretKey) throws BeanstalkAdapterException {

    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);

    AmazonS3 s3client = new AmazonS3Client(basic_credentials);

    try {//from   w  ww . j a  v a2  s  .  c  o  m
        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());

        throw new BeanstalkAdapterException(ase.getMessage());

    } 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());
        throw new BeanstalkAdapterException(ace.getMessage());

    }
}