Search in sources :

Example 21 with AmazonS3

use of com.amazonaws.services.s3.AmazonS3 in project jackrabbit-oak by apache.

the class Utils method deleteBucket.

/**
     * Delete S3 bucket. This method first deletes all objects from bucket and
     * then delete empty bucket.
     * 
     * @param bucketName the bucket name.
     */
public static void deleteBucket(final String bucketName) throws IOException {
    Properties prop = readConfig(DEFAULT_CONFIG_FILE);
    AmazonS3 s3service = openService(prop);
    ObjectListing prevObjectListing = s3service.listObjects(bucketName);
    while (true) {
        for (S3ObjectSummary s3ObjSumm : prevObjectListing.getObjectSummaries()) {
            s3service.deleteObject(bucketName, s3ObjSumm.getKey());
        }
        if (!prevObjectListing.isTruncated()) {
            break;
        }
        prevObjectListing = s3service.listNextBatchOfObjects(prevObjectListing);
    }
    s3service.deleteBucket(bucketName);
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) Properties(java.util.Properties)

Example 22 with AmazonS3

use of com.amazonaws.services.s3.AmazonS3 in project aws-doc-sdk-examples by awsdocs.

the class GetBucketPolicy method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    GetBucketPolicy <bucket>\n\n" + "Where:\n" + "    bucket - the bucket to get the policy from.\n\n" + "Example:\n" + "    GetBucketPolicy testbucket\n\n";
    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucket_name = args[0];
    String policy_text = null;
    System.out.format("Getting policy for bucket: \"%s\"\n\n", bucket_name);
    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {
        BucketPolicy bucket_policy = s3.getBucketPolicy(bucket_name);
        policy_text = bucket_policy.getPolicyText();
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    if (policy_text == null) {
        System.out.println("The specified bucket has no bucket policy.");
    } else {
        System.out.println("Returned policy:");
        System.out.println("----");
        System.out.println(policy_text);
        System.out.println("----\n");
    }
    System.out.println("Done!");
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) BucketPolicy(com.amazonaws.services.s3.model.BucketPolicy) AmazonServiceException(com.amazonaws.AmazonServiceException)

Example 23 with AmazonS3

use of com.amazonaws.services.s3.AmazonS3 in project aws-doc-sdk-examples by awsdocs.

the class GetObject method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket and object to\n" + "download from it.\n" + "\n" + "Ex: GetObject <bucketname> <filename>\n";
    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucket_name = args[0];
    String key_name = args[1];
    System.out.format("Downloading %s from S3 bucket %s...\n", key_name, bucket_name);
    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {
        S3Object o = s3.getObject(bucket_name, key_name);
        S3ObjectInputStream s3is = o.getObjectContent();
        FileOutputStream fos = new FileOutputStream(new File(key_name));
        byte[] read_buf = new byte[1024];
        int read_len = 0;
        while ((read_len = s3is.read(read_buf)) > 0) {
            fos.write(read_buf, 0, read_len);
        }
        s3is.close();
        fos.close();
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    } catch (FileNotFoundException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) FileOutputStream(java.io.FileOutputStream) AmazonServiceException(com.amazonaws.AmazonServiceException) FileNotFoundException(java.io.FileNotFoundException) S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) S3Object(com.amazonaws.services.s3.model.S3Object) IOException(java.io.IOException) File(java.io.File)

Example 24 with AmazonS3

use of com.amazonaws.services.s3.AmazonS3 in project aws-doc-sdk-examples by awsdocs.

the class GetWebsiteConfiguration method getWebsiteConfig.

public static void getWebsiteConfig(String bucket_name) {
    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {
        BucketWebsiteConfiguration config = s3.getBucketWebsiteConfiguration(bucket_name);
        if (config == null) {
            System.out.println("No website configuration found!");
        } else {
            System.out.format("Index document: %s\n", config.getIndexDocumentSuffix());
            System.out.format("Error document: %s\n", config.getErrorDocument());
        }
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.out.println("Failed to get website configuration!");
        System.exit(1);
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) BucketWebsiteConfiguration(com.amazonaws.services.s3.model.BucketWebsiteConfiguration) AmazonServiceException(com.amazonaws.AmazonServiceException)

Example 25 with AmazonS3

use of com.amazonaws.services.s3.AmazonS3 in project aws-doc-sdk-examples by awsdocs.

the class ListBuckets method main.

public static void main(String[] args) {
    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    List<Bucket> buckets = s3.listBuckets();
    System.out.println("Your Amazon S3 buckets are:");
    for (Bucket b : buckets) {
        System.out.println("* " + b.getName());
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) Bucket(com.amazonaws.services.s3.model.Bucket)

Aggregations

AmazonS3 (com.amazonaws.services.s3.AmazonS3)50 AmazonServiceException (com.amazonaws.AmazonServiceException)15 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)9 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)9 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)9 Bucket (com.amazonaws.services.s3.model.Bucket)6 ClientConfiguration (com.amazonaws.ClientConfiguration)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)4 AccessControlList (com.amazonaws.services.s3.model.AccessControlList)4 Configuration (org.apache.hadoop.conf.Configuration)4 AWSCredentials (com.amazonaws.auth.AWSCredentials)3 S3ClientOptions (com.amazonaws.services.s3.S3ClientOptions)3 S3Object (com.amazonaws.services.s3.model.S3Object)3 AmazonClientException (com.amazonaws.AmazonClientException)2 BucketWebsiteConfiguration (com.amazonaws.services.s3.model.BucketWebsiteConfiguration)2 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)2 EmailAddressGrantee (com.amazonaws.services.s3.model.EmailAddressGrantee)2 Grant (com.amazonaws.services.s3.model.Grant)2