List of usage examples for com.amazonaws.services.s3 AmazonS3Client AmazonS3Client
@Deprecated
public AmazonS3Client()
From source file:aws.example.s3.CopyObject.java
License:Open Source License
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);/*from www . ja v a 2 s . co m*/ } 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!"); }
From source file:aws.example.s3.CreateBucket.java
License:Open Source License
public static void main(String[] args) { final String USAGE = "\n" + "To run this example, supply the name of a bucket to create!\n" + "Ex: CreateBucket <unique-bucket-name>\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1);// w ww .ja va 2s. c o m } String bucket_name = args[0]; System.out.println("Creating S3 bucket: " + bucket_name); final AmazonS3 s3 = new AmazonS3Client(); try { Bucket b = s3.createBucket(bucket_name); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
From source file:aws.example.s3.DeleteBucket.java
License:Open Source License
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);/* www . java 2s . c o m*/ } String bucket_name = args[0]; System.out.println("Deleting S3 bucket: " + bucket_name); final AmazonS3 s3 = new AmazonS3Client(); 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!"); }
From source file:aws.example.s3.DeleteObject.java
License:Open Source License
public static void main(String[] args) { final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket and object\n" + "name (key) to delete.\n" + "\n" + "Ex: DeleteObject <bucketname> <objectname>\n"; if (args.length < 2) { System.out.println(USAGE); System.exit(1);/* www.j ava 2s.c o m*/ } String bucket_name = args[0]; String object_key = args[1]; System.out.format("Deleting object %s from S3 bucket: %s\n", object_key, bucket_name); final AmazonS3 s3 = new AmazonS3Client(); try { s3.deleteObject(bucket_name, object_key); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
From source file:aws.example.s3.DeleteObjects.java
License:Open Source License
public static void main(String[] args) { final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket and at least\n" + "one object name (key) to delete.\n" + "\n" + "Ex: DeleteObjects <bucketname> <objectname1> [objectname2, ...]\n"; if (args.length < 2) { System.out.println(USAGE); System.exit(1);//from www . j a va 2 s .co m } String bucket_name = args[0]; String[] object_keys = Arrays.copyOfRange(args, 1, args.length); System.out.println("Deleting objects from S3 bucket: " + bucket_name); for (String k : object_keys) { System.out.println(" * " + k); } final AmazonS3 s3 = new AmazonS3Client(); try { DeleteObjectsRequest dor = new DeleteObjectsRequest(bucket_name).withKeys(object_keys); s3.deleteObjects(dor); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
From source file:aws.example.s3.GetObject.java
License:Open Source License
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);/* ww w . j av a 2 s. com*/ } 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 = new AmazonS3Client(); 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!"); }
From source file:aws.example.s3.ListBuckets.java
License:Open Source License
public static void main(String[] args) { final AmazonS3 s3 = new AmazonS3Client(); List<Bucket> buckets = s3.listBuckets(); System.out.println("Your Amazon S3 buckets:"); for (Bucket b : buckets) { System.out.println("* " + b.getName()); }/*w w w. j a v a 2s. co m*/ }
From source file:aws.example.s3.ListObjects.java
License:Open Source License
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);/* w ww. j a v a2 s. c o m*/ } String bucket_name = args[0]; System.out.format("Objects in S3 bucket %s:\n", bucket_name); final AmazonS3 s3 = new AmazonS3Client(); ObjectListing ol = s3.listObjects(bucket_name); List<S3ObjectSummary> objects = ol.getObjectSummaries(); for (S3ObjectSummary os : objects) { System.out.println("* " + os.getKey()); } }
From source file:aws.example.s3.PutObject.java
License:Open Source License
public static void main(String[] args) { final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket and a file to\n" + "upload to it.\n" + "\n" + "Ex: PutObject <bucketname> <filename>\n"; if (args.length < 2) { System.out.println(USAGE); System.exit(1);//from ww w . ja v a 2 s.c o m } String bucket_name = args[0]; String file_path = args[1]; String key_name = Paths.get(file_path).getFileName().toString(); System.out.format("Uploading %s to S3 bucket %s...\n", file_path, bucket_name); final AmazonS3 s3 = new AmazonS3Client(); try { s3.putObject(bucket_name, key_name, file_path); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
From source file:awslabs.lab51.SolutionCode.java
License:Open Source License
@Override public AmazonS3Client createS3Client(AWSCredentials credentials) { Region region = Region.getRegion(Regions.fromName(System.getProperty("REGION"))); AmazonS3Client client = new AmazonS3Client(); client.setRegion(region);/*w w w . j a va 2s . co m*/ return client; }