use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class CopyObject method copyBucketObject.
// snippet-start:[s3.java2.copy_object.main]
public static String copyBucketObject(S3Client s3, String fromBucket, String objectKey, String toBucket) {
String encodedUrl = null;
try {
encodedUrl = URLEncoder.encode(fromBucket + "/" + objectKey, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
System.out.println("URL could not be encoded: " + e.getMessage());
}
CopyObjectRequest copyReq = CopyObjectRequest.builder().copySource(encodedUrl).destinationBucket(toBucket).destinationKey(objectKey).build();
try {
CopyObjectResponse copyRes = s3.copyObject(copyReq);
return copyRes.copyObjectResult().toString();
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class CreateBucket method createBucket.
// snippet-start:[s3.java2.create_bucket_waiters.main]
public static void createBucket(S3Client s3Client, String bucketName) {
try {
S3Waiter s3Waiter = s3Client.waiter();
CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).build();
s3Client.createBucket(bucketRequest);
HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder().bucket(bucketName).build();
// Wait until the bucket is created and print out the response.
WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
waiterResponse.matched().response().ifPresent(System.out::println);
System.out.println(bucketName + " is ready");
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class DeleteBucketPolicy method deleteS3BucketPolicy.
// snippet-start:[s3.java2.delete_bucket_policy.main]
// Delete the bucket policy
public static void deleteS3BucketPolicy(S3Client s3, String bucketName) {
DeleteBucketPolicyRequest delReq = DeleteBucketPolicyRequest.builder().bucket(bucketName).build();
try {
s3.deleteBucketPolicy(delReq);
System.out.println("Done!");
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class ExportEndpoints method downloadFromS3.
// Downloads files from an Amazon S3 bucket and writes them to the path location
public static void downloadFromS3(S3Client s3Client, String path, String s3BucketName, List<String> objectKeys) {
try {
for (String key : objectKeys) {
GetObjectRequest objectRequest = GetObjectRequest.builder().bucket(s3BucketName).key(key).build();
ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
byte[] data = objectBytes.asByteArray();
// Write the data to a local file
String fileSuffix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
path = path + fileSuffix + ".gz";
File myFile = new File(path);
OutputStream os = new FileOutputStream(myFile);
os.write(data);
}
System.out.println("Download finished.");
} catch (S3Exception | NullPointerException | IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class PPEBoundingBoxFrame 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;
}
Aggregations