use of com.amazonaws.services.s3.AmazonS3 in project aws-doc-sdk-examples by awsdocs.
the class ListObjects method main.
public static void main(String[] args) {
final String USAGE = "\n" + "To run this example, supply the name of a bucket to list!\n" + "\n" + "Ex: ListObjects <bucket-name>\n";
if (args.length < 1) {
System.out.println(USAGE);
System.exit(1);
}
String bucket_name = args[0];
System.out.format("Objects in S3 bucket %s:\n", bucket_name);
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
ObjectListing ol = s3.listObjects(bucket_name);
List<S3ObjectSummary> objects = ol.getObjectSummaries();
for (S3ObjectSummary os : objects) {
System.out.println("* " + os.getKey());
}
}
use of com.amazonaws.services.s3.AmazonS3 in project aws-doc-sdk-examples by awsdocs.
the class CopyObject method main.
public static void main(String[] args) {
final String USAGE = "\n" + "To run this example, supply the name (key) of an S3 object, the bucket name\n" + "that it's contained within, and the bucket to copy it to.\n" + "\n" + "Ex: CopyObject <objectname> <frombucket> <tobucket>\n";
if (args.length < 3) {
System.out.println(USAGE);
System.exit(1);
}
String object_key = args[0];
String from_bucket = args[1];
String to_bucket = args[2];
System.out.format("Copying object %s from bucket %s to %s\n", object_key, from_bucket, to_bucket);
final AmazonS3 s3 = new AmazonS3Client();
try {
s3.copyObject(from_bucket, object_key, to_bucket, object_key);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("Done!");
}
use of com.amazonaws.services.s3.AmazonS3 in project aws-doc-sdk-examples by awsdocs.
the class CreateBucket method getBucket.
public static Bucket getBucket(String bucket_name) {
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
Bucket named_bucket = null;
List<Bucket> buckets = s3.listBuckets();
for (Bucket b : buckets) {
if (b.getName().equals(bucket_name)) {
named_bucket = b;
}
}
return named_bucket;
}
use of com.amazonaws.services.s3.AmazonS3 in project aws-doc-sdk-examples by awsdocs.
the class DeleteBucket method main.
public static void main(String[] args) {
final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket\n" + "\n" + "Ex: DeleteBucket <bucketname>\n";
if (args.length < 1) {
System.out.println(USAGE);
System.exit(1);
}
String bucket_name = args[0];
System.out.println("Deleting S3 bucket: " + bucket_name);
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
try {
System.out.println(" - removing objects from bucket");
ObjectListing object_listing = s3.listObjects(bucket_name);
while (true) {
for (Iterator<?> iterator = object_listing.getObjectSummaries().iterator(); iterator.hasNext(); ) {
S3ObjectSummary summary = (S3ObjectSummary) iterator.next();
s3.deleteObject(bucket_name, summary.getKey());
}
// more object_listing to retrieve?
if (object_listing.isTruncated()) {
object_listing = s3.listNextBatchOfObjects(object_listing);
} else {
break;
}
}
;
System.out.println(" - removing versions from bucket");
VersionListing version_listing = s3.listVersions(new ListVersionsRequest().withBucketName(bucket_name));
while (true) {
for (Iterator<?> iterator = version_listing.getVersionSummaries().iterator(); iterator.hasNext(); ) {
S3VersionSummary vs = (S3VersionSummary) iterator.next();
s3.deleteVersion(bucket_name, vs.getKey(), vs.getVersionId());
}
if (version_listing.isTruncated()) {
version_listing = s3.listNextBatchOfVersions(version_listing);
} else {
break;
}
}
System.out.println(" OK, bucket ready to delete!");
s3.deleteBucket(bucket_name);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("Done!");
}
use of com.amazonaws.services.s3.AmazonS3 in project aws-doc-sdk-examples by awsdocs.
the class SetWebsiteConfiguration method setWebsiteConfig.
public static void setWebsiteConfig(String bucket_name, String index_doc, String error_doc) {
BucketWebsiteConfiguration website_config = null;
if (index_doc == null) {
website_config = new BucketWebsiteConfiguration();
} else if (error_doc == null) {
website_config = new BucketWebsiteConfiguration(index_doc);
} else {
website_config = new BucketWebsiteConfiguration(index_doc, error_doc);
}
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
try {
s3.setBucketWebsiteConfiguration(bucket_name, website_config);
} catch (AmazonServiceException e) {
System.out.format("Failed to set website configuration for bucket '%s'!\n", bucket_name);
System.err.println(e.getErrorMessage());
System.exit(1);
}
}
Aggregations