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

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

Introduction

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

Prototype

public void setRegion(com.amazonaws.regions.Region region) throws IllegalArgumentException;

Source Link

Document

An alternative to AmazonS3#setEndpoint(String) , sets the regional endpoint for this client's service calls.

Usage

From source file:S3Controller.UploadImages.java

public void UploadToS3(String bucketname, String filename, String filepath) {
    //  public static void main(String[] args) {

    AWSCredentials credentials = null;//from   ww  w .j  a  va2 s.c o  m
    String aws_access_key_id = "PUT_YOUR_aws_access_key_id_HERE";
    String aws_secret_access_key = "PUT_YOUR_aws_secret_access_key_HERE";
    String bucketName = bucketname; //"zillionbucket";
    String fileName = filename; //"javaee_duke_image.jpg";
    String localpath = filepath; //"C:\\Users\\Hariom\\Documents\\NetBeansProjects\\JavaApplication9\\src\\ProcessedImages\\javaee_duke_image.jpg";
    try {
        credentials = new BasicAWSCredentials(aws_access_key_id, aws_secret_access_key);//.getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }

    AmazonS3 s3 = new AmazonS3Client(credentials);
    Region AP_SOUTHEAST_1 = Region.getRegion(Regions.AP_SOUTHEAST_1);
    s3.setRegion(AP_SOUTHEAST_1);

    try {

        s3.putObject(new PutObjectRequest(bucketName, fileName, new File(localpath))
                .withCannedAcl(CannedAccessControlList.PublicRead));

        System.out.println("File Uploaded to S3: " + fileName);

    }

    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:upload.s3.S3TransferProgressSample.java

License:Open Source License

public S3TransferProgressSample(File file) throws Exception {
    AmazonS3 s3 = new AmazonS3Client(
            credentials = new ClasspathPropertiesFileCredentialsProvider().getCredentials());
    Region usWest2 = Region.getRegion(Regions.EU_WEST_1);
    s3.setRegion(usWest2);
    tx = new TransferManager(s3);

    bucketName = "test-s3-sqs";

    createAmazonS3Bucket();/*w w w  .jav a 2  s .com*/

    PutObjectRequest request = new PutObjectRequest(bucketName, file.getName(), file)
            .withCannedAcl(CannedAccessControlList.PublicRead);
    upload = tx.upload(request);
}