use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class DeleteMultiObjects method deleteBucketObjects.
// snippet-start:[s3.java2.delete_many_objects.main]
public static void deleteBucketObjects(S3Client s3, String bucketName) {
// Upload three sample objects to the specfied Amazon S3 bucket.
ArrayList<ObjectIdentifier> keys = new ArrayList<>();
PutObjectRequest putOb = null;
ObjectIdentifier objectId = null;
for (int i = 0; i < 3; i++) {
String keyName = "delete object example " + i;
objectId = ObjectIdentifier.builder().key(keyName).build();
putOb = PutObjectRequest.builder().bucket(bucketName).key(keyName).build();
s3.putObject(putOb, RequestBody.fromString(keyName));
keys.add(objectId);
}
System.out.println(keys.size() + " objects successfully created.");
// Delete multiple objects in one request.
Delete del = Delete.builder().objects(keys).build();
try {
DeleteObjectsRequest multiObjectDeleteRequest = DeleteObjectsRequest.builder().bucket(bucketName).delete(del).build();
s3.deleteObjects(multiObjectDeleteRequest);
System.out.println("Multiple objects are deleted!");
} 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 DisplayFacesFrame method getObjectBytes.
public static byte[] getObjectBytes(S3Client s3, String bucketName, String keyName) {
try {
GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
byte[] data = objectBytes.asByteArray();
return data;
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return null;
}
use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class DeleteObjects method main.
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: <bucketName> <objectName>\n";
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String objectName = args[1];
System.out.println("Deleting an object from the Amazon S3 bucket: " + bucketName);
Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder().region(region).build();
deleteBucketObjects(s3, bucketName, objectName);
s3.close();
}
use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class GetAcl method getBucketACL.
// snippet-start:[s3.java2.get_acl.main]
public static String getBucketACL(S3Client s3, String objectKey, String bucketName) {
try {
GetObjectAclRequest aclReq = GetObjectAclRequest.builder().bucket(bucketName).key(objectKey).build();
GetObjectAclResponse aclRes = s3.getObjectAcl(aclReq);
List<Grant> grants = aclRes.grants();
String grantee = "";
for (Grant grant : grants) {
System.out.format(" %s: %s\n", grant.grantee().id(), grant.permission());
grantee = grant.grantee().id();
}
return grantee;
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class GetObjectUrl method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " <bucketName> <keyName> \n\n" + "Where:\n" + " bucketName - the Amazon S3 bucket name.\n\n" + " keyName - a key name that represents the object. \n\n";
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String keyName = args[1];
Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder().region(region).build();
getURL(s3, bucketName, keyName);
s3.close();
}
Aggregations