use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class LifecycleConfiguration method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " <bucketName> <accountId> \n\n" + "Where:\n" + " bucketName - the Amazon Simple Storage Service (Amazon S3) bucket to upload an object into.\n" + " accountId - the id of the account that owns the Amazon S3 bucket.\n";
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String accountId = args[1];
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder().region(region).build();
setLifecycleConfig(s3, bucketName, accountId);
getLifecycleConfig(s3, bucketName, accountId);
deleteLifecycleConfig(s3, bucketName, accountId);
System.out.println("You have successfully created, updated, and deleted a Lifecycle configuration ");
s3.close();
}
use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class ListObjects method listBucketObjects.
// snippet-start:[s3.java2.list_objects.main]
public static void listBucketObjects(S3Client s3, String bucketName) {
try {
ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
ListObjectsResponse res = s3.listObjects(listObjects);
List<S3Object> objects = res.contents();
for (ListIterator iterVals = objects.listIterator(); iterVals.hasNext(); ) {
S3Object myValue = (S3Object) iterVals.next();
System.out.print("\n The name of the key is " + myValue.key());
System.out.print("\n The object is " + calKb(myValue.size()) + " KBs");
System.out.print("\n The owner is " + myValue.owner());
}
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class AbortMultipartUpload method abortUploads.
// snippet-start:[s3.java2.abort_upload.main]
public static void abortUploads(S3Client s3, String bucketName, String accountId) {
try {
ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder().bucket(bucketName).build();
ListMultipartUploadsResponse response = s3.listMultipartUploads(listMultipartUploadsRequest);
List<MultipartUpload> uploads = response.uploads();
AbortMultipartUploadRequest abortMultipartUploadRequest = null;
for (MultipartUpload upload : uploads) {
abortMultipartUploadRequest = AbortMultipartUploadRequest.builder().bucket(bucketName).key(upload.key()).expectedBucketOwner(accountId).uploadId(upload.uploadId()).build();
s3.abortMultipartUpload(abortMultipartUploadRequest);
}
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class AbortMultipartUpload method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " <bucketName> <accountId> \n\n" + "Where:\n" + " bucketName - the Amazon Simple Storage Service (Amazon S3) bucket.\n" + " accountId - the id of the account that owns the Amazon S3 bucket.\n";
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String accountId = args[1];
Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder().region(region).build();
abortUploads(s3, bucketName, accountId);
s3.close();
}
use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class GetObjectData method getObjectBytes.
// snippet-start:[s3.java2.getobjectdata.main]
public static void getObjectBytes(S3Client s3, String bucketName, String keyName, String path) {
try {
GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
byte[] data = objectBytes.asByteArray();
// Write the data to a local file
File myFile = new File(path);
OutputStream os = new FileOutputStream(myFile);
os.write(data);
System.out.println("Successfully obtained bytes from an S3 object");
os.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
Aggregations