Example usage for com.amazonaws.services.s3.model CreateBucketRequest CreateBucketRequest

List of usage examples for com.amazonaws.services.s3.model CreateBucketRequest CreateBucketRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model CreateBucketRequest CreateBucketRequest.

Prototype

public CreateBucketRequest(String bucketName) 

Source Link

Document

Constructs a new CreateBucketRequest , ready to be executed to create the specified bucket in the US_Standard region.

Usage

From source file:awslabs.lab21.SolutionCode.java

License:Open Source License

@Override
public void createBucket(AmazonS3 s3Client, String bucketName, Region region) {
    // Construct a CreateBucketRequest object that contains the provided bucket name.
    // If the region is other than us-east-1, we need to specify a regional constraint.
    CreateBucketRequest createBucketRequest;
    if (region.getName().equals("us-east-1")) {
        createBucketRequest = new CreateBucketRequest(bucketName);
    } else {/*from w w  w .  j av  a2  s. co m*/
        createBucketRequest = new CreateBucketRequest(bucketName,
                com.amazonaws.services.s3.model.Region.fromValue(region.getName()));
    }

    // Submit the request using the createBucket method of the s3Client object.
    s3Client.createBucket(createBucketRequest);

}

From source file:awslabs.lab41.SolutionCode.java

License:Open Source License

@Override
public void prepMode_CreateBucket(AmazonS3Client s3Client, String bucketName, Region region) {
    // Construct a CreateBucketRequest object that contains the provided bucket name.
    // If the region is other than us-east-1, we need to specify a regional constraint.
    CreateBucketRequest createBucketRequest;
    if (region.getName().equals("us-east-1")) {
        createBucketRequest = new CreateBucketRequest(bucketName);
    } else {//from w  w  w  . j a v a  2 s. c om
        createBucketRequest = new CreateBucketRequest(bucketName,
                com.amazonaws.services.s3.model.Region.fromValue(region.getName()));
    }
    s3Client.createBucket(createBucketRequest);
}

From source file:cloudExplorer.BucketClass.java

License:Open Source License

String makeBucket(String access_key, String secret_key, String bucket, String endpoint, String region) {
    String message = null;/*from  ww  w .j  av a 2 s .  c  o m*/
    AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
    AmazonS3 s3Client = new AmazonS3Client(credentials,
            new ClientConfiguration().withSignerOverride("S3SignerType"));
    s3Client.setEndpoint(endpoint);
    try {
        if (endpoint.contains("amazon")) {
            s3Client.createBucket(new CreateBucketRequest(bucket));

        } else {
            s3Client.createBucket(new CreateBucketRequest(bucket, region));
        }

        message = ("\nAttempting to create the bucket. Please view the Bucket list window for an update.");
    } catch (AmazonServiceException ase) {
        if (NewJFrame.gui) {
            mainFrame.jTextArea1.append("\n\nError Message:    " + ase.getMessage());
            mainFrame.jTextArea1.append("\nHTTP Status Code: " + ase.getStatusCode());
            mainFrame.jTextArea1.append("\nAWS Error Code:   " + ase.getErrorCode());
            mainFrame.jTextArea1.append("\nError Type:       " + ase.getErrorType());
            mainFrame.jTextArea1.append("\nRequest ID:       " + ase.getRequestId());
            calibrate();
        } else {
            System.out.print("\n\nError Message:    " + ase.getMessage());
            System.out.print("\nHTTP Status Code: " + ase.getStatusCode());
            System.out.print("\nAWS Error Code:   " + ase.getErrorCode());
            System.out.print("\nError Type:       " + ase.getErrorType());
            System.out.print("\nRequest ID:       " + ase.getRequestId());
        }
    }
    if (message == null) {
        message = "Failed to create bucket.";
    }
    return message;

}

From source file:com.amazon.services.awsrum.utils.S3Utils.java

License:Open Source License

/**
 * Create a S3 bucket if it does not exist.
 * // ww  w.  ja  va  2  s.  c o  m
 * @param client
 *            The {@link AmazonS3Client} with read and write permissions
 * @param bucketName
 *            The bucket to create
 * @throws IllegalStateException
 *             The bucket is not created before timeout occurs
 */
public static void createBucket(AmazonS3Client client, String bucketName) {
    if (!bucketExists(client, bucketName)) {
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
        createBucketRequest.setRegion(Region.US_Standard.toString());
        client.createBucket(createBucketRequest);
    }
    long startTime = System.currentTimeMillis();
    long endTime = startTime + 60 * 1000;
    while (!bucketExists(client, bucketName) && endTime > System.currentTimeMillis()) {
        try {
            LOG.info("Waiting for S3 to create bucket " + bucketName);
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
        }
    }
    if (!bucketExists(client, bucketName)) {
        throw new IllegalStateException("Could not create bucket " + bucketName);
    }
    LOG.info("Created S3 bucket " + bucketName);
}

From source file:com.amazon.util.ImageUploader.java

public static void uploadImage(String imageURL, String imageName, String folderName, String bucketName)
        throws MalformedURLException, IOException {
    // credentials object identifying user for authentication

    AWSCredentials credentials = new BasicAWSCredentials(System.getenv("AWS_S3_ACCESS_KEY"),
            System.getenv("AWS_S3_SECRET_ACCESS_KEY"));

    // create a client connection based on credentials
    AmazonS3 s3client = new AmazonS3Client(credentials);

    try {/*from   w w w.jav a2s.  c o  m*/
        if (!(s3client.doesBucketExist(bucketName))) {
            s3client.setRegion(Region.getRegion(Regions.US_EAST_1));
            // Note that CreateBucketRequest does not specify region. So bucket is 
            // created in the region specified in the client.
            s3client.createBucket(new CreateBucketRequest(bucketName));
        }

        //Enabe CORS:
        //     <?xml version="1.0" encoding="UTF-8"?>
        //<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
        //    <CORSRule>
        //        <AllowedOrigin>http://ask-ifr-download.s3.amazonaws.com</AllowedOrigin>
        //        <AllowedMethod>GET</AllowedMethod>
        //    </CORSRule>
        //</CORSConfiguration>
        BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration();

        CORSRule corsRule = new CORSRule()
                .withAllowedMethods(
                        Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.GET }))
                .withAllowedOrigins(Arrays.asList(new String[] { "http://ask-ifr-download.s3.amazonaws.com" }));
        configuration.setRules(Arrays.asList(new CORSRule[] { corsRule }));
        s3client.setBucketCrossOriginConfiguration(bucketName, configuration);

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

    String fileName = folderName + SUFFIX + imageName + ".png";
    URL url = new URL(imageURL);

    ObjectMetadata omd = new ObjectMetadata();
    omd.setContentType("image/png");
    omd.setContentLength(url.openConnection().getContentLength());
    // upload file to folder and set it to public
    s3client.putObject(new PutObjectRequest(bucketName, fileName, url.openStream(), omd)
            .withCannedAcl(CannedAccessControlList.PublicRead));
}

From source file:com.example.utils.S3Utils.java

License:Open Source License

/**
 * Create an Amazon S3 bucket if it does not exist.
 * //from w  w  w . j a  v  a2  s .c  om
 * @param client
 *        The {@link AmazonS3Client} with read and write permissions
 * @param bucketName
 *        The bucket to create
 * @throws IllegalStateException
 *         The bucket is not created before timeout occurs
 */
public static void createBucket(AmazonS3Client client, String bucketName) {
    if (!bucketExists(client, bucketName)) {
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
        createBucketRequest.setRegion(Region.US_Standard.toString());
        client.createBucket(createBucketRequest);
    }
    long startTime = System.currentTimeMillis();
    long endTime = startTime + 60 * 1000;
    while (!bucketExists(client, bucketName) && endTime > System.currentTimeMillis()) {
        try {
            LOG.info("Waiting for Amazon S3 to create bucket " + bucketName);
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
        }
    }
    if (!bucketExists(client, bucketName)) {
        throw new IllegalStateException("Could not create bucket " + bucketName);
    }
    LOG.info("Created Amazon S3 bucket " + bucketName);
}

From source file:com.github.abhinavmishra14.aws.s3.service.impl.AwsS3IamServiceImpl.java

License:Open Source License

@Override
public Bucket createBucket(final String bucketName, final CannedAccessControlList cannedAcl)
        throws AmazonClientException, AmazonServiceException {
    LOGGER.info("createBucket invoked, bucketName: {} and cannedAccessControlList: {}", bucketName, cannedAcl);
    final CreateBucketRequest createBucketReq = new CreateBucketRequest(bucketName).withCannedAcl(cannedAcl);
    return s3client.createBucket(createBucketReq);
}

From source file:com.github.abhinavmishra14.aws.s3.service.impl.AwsS3IamServiceImpl.java

License:Open Source License

@Override
public Bucket createBucket(final String bucketName, final boolean isPublicAccessible)
        throws AmazonClientException, AmazonServiceException {
    LOGGER.info("createBucket invoked, bucketName: {} and isPublicAccessible: {}", bucketName,
            isPublicAccessible);/*w  w  w .  ja  v  a  2s .  co m*/
    final CreateBucketRequest createBucketReq = new CreateBucketRequest(bucketName);
    if (isPublicAccessible) {
        createBucketReq.setCannedAcl(CannedAccessControlList.PublicRead);
    }
    return s3client.createBucket(createBucketReq);
}

From source file:com.ipcglobal.fredimportaws.TsvsToRedshift.java

License:Apache License

/**
 * Process.//from ww w .  j  a  v  a2s  .co  m
 *
 * @throws Exception the exception
 */
public void process() throws Exception {
    try {
        s3Client.createBucket(new CreateBucketRequest(awsBucketName));
        log.info("Start: emptyBucketContents");
        long before = System.currentTimeMillis();
        emptyBucketContents();
        log.info("Complete: emptyBucketContents, elapsed=" + (System.currentTimeMillis() - before));

        log.info("Start: transferToBucket");
        before = System.currentTimeMillis();
        transferToBucket();
        log.info("Complete: transferToBucket, elapsed=" + (System.currentTimeMillis() - before));

        log.info("Start: copyS3FilesToRedshiftTable");
        before = System.currentTimeMillis();
        copyS3FilesToRedshiftTable();
        log.info("Complete: copyS3FilesToRedshiftTable, elapsed=" + (System.currentTimeMillis() - before));

    } catch (AmazonServiceException ase) {
        log.error("Caught Exception: " + ase.getMessage());
        log.error("Reponse Status Code: " + ase.getStatusCode());
        log.error("Error Code: " + ase.getErrorCode());
        log.error("Request ID: " + ase.getRequestId());
        throw ase;
    } catch (AmazonClientException ace) {
        log.error("Error Message: " + ace.getMessage());
        throw ace;
    } catch (Exception e) {
        log.error(e);
        throw e;
    }
}

From source file:com.maya.portAuthority.util.ImageUploader.java

public static void uploadImage(String imageURL, String imageName, String bucketName)
         throws MalformedURLException, IOException {
     // credentials object identifying user for authentication

     AWSCredentials credentials = new BasicAWSCredentials("AKIAJBFSMHRTIQQ7BKYA",
             "AdHgeP4dyWInWwPn9YlfxFCm3qP1lHjdxOxeJqDa");

     // create a client connection based on credentials
     AmazonS3 s3client = new AmazonS3Client(credentials);

     String folderName = "image"; //folder name
     //    String bucketName = "ppas-image-upload"; //must be unique

     try {//from w w w  .  jav a  2s.  c o  m
         if (!(s3client.doesBucketExist(bucketName))) {
             s3client.setRegion(Region.getRegion(Regions.US_EAST_1));
             // Note that CreateBucketRequest does not specify region. So bucket is 
             // created in the region specified in the client.
             s3client.createBucket(new CreateBucketRequest(bucketName));
         }

         //Enabe CORS:
         //     <?xml version="1.0" encoding="UTF-8"?>
         //<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
         //    <CORSRule>
         //        <AllowedOrigin>http://ask-ifr-download.s3.amazonaws.com</AllowedOrigin>
         //        <AllowedMethod>GET</AllowedMethod>
         //    </CORSRule>
         //</CORSConfiguration>
         BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration();

         CORSRule corsRule = new CORSRule()
                 .withAllowedMethods(
                         Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.GET }))
                 .withAllowedOrigins(Arrays.asList(new String[] { "http://ask-ifr-download.s3.amazonaws.com" }));
         configuration.setRules(Arrays.asList(new CORSRule[] { corsRule }));
         s3client.setBucketCrossOriginConfiguration(bucketName, configuration);

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

     String fileName = folderName + SUFFIX + imageName + ".png";
     URL url = new URL(imageURL);

     ObjectMetadata omd = new ObjectMetadata();
     omd.setContentType("image/png");
     omd.setContentLength(url.openConnection().getContentLength());
     // upload file to folder and set it to public
     s3client.putObject(new PutObjectRequest(bucketName, fileName, url.openStream(), omd)
             .withCannedAcl(CannedAccessControlList.PublicRead));
 }