use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class GeneratePresignedUrlUploadImage method signBucket.
// snippet-start:[presigned.java2.generatepresignedurlimage.main]
public static void signBucket(S3Presigner presigner, String bucketName, String keyName, byte[] pic) {
try {
PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(keyName).contentType("image/png").build();
PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder().signatureDuration(Duration.ofMinutes(10)).putObjectRequest(objectRequest).build();
PresignedPutObjectRequest presignedRequest = presigner.presignPutObject(presignRequest);
String myURL = presignedRequest.url().toString();
System.out.println("Presigned URL to upload a file to: " + myURL);
System.out.println("Which HTTP method needs to be used when uploading a file: " + presignedRequest.httpRequest().method());
// Upload content to the Amazon S3 bucket by using this URL
URL url = presignedRequest.url();
// Create the connection and use it to upload the new object by using the presigned URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "image/png");
connection.setRequestMethod("PUT");
connection.getOutputStream().write(pic);
connection.getResponseCode();
System.out.println("HTTP response code is " + connection.getResponseCode());
} catch (S3Exception e) {
e.getStackTrace();
} catch (IOException e) {
e.getStackTrace();
}
}
use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class GetBucketPolicy method getPolicy.
// snippet-start:[s3.java2.get_bucket_policy.main]
public static String getPolicy(S3Client s3, String bucketName) {
String policyText = "";
System.out.format("Getting policy for bucket: \"%s\"\n\n", bucketName);
GetBucketPolicyRequest policyReq = GetBucketPolicyRequest.builder().bucket(bucketName).build();
try {
GetBucketPolicyResponse policyRes = s3.getBucketPolicy(policyReq);
policyText = policyRes.policy();
return policyText;
} 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 GetObjectTags method listTags.
// snippet-start:[s3.java2.getobjecttags.main]
public static void listTags(S3Client s3, String bucketName, String keyName) {
try {
GetObjectTaggingRequest getTaggingRequest = GetObjectTaggingRequest.builder().key(keyName).bucket(bucketName).build();
GetObjectTaggingResponse tags = s3.getObjectTagging(getTaggingRequest);
List<Tag> tagSet = tags.tagSet();
// Write out the tags
Iterator<Tag> tagIterator = tagSet.iterator();
while (tagIterator.hasNext()) {
Tag tag = (Tag) tagIterator.next();
System.out.println(tag.key());
System.out.println(tag.value());
}
} 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 ListMultipartUploads method listUploads.
// snippet-start:[s3.java2.list_multi_uploads.main]
public static void listUploads(S3Client s3, String bucketName) {
try {
ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder().bucket(bucketName).build();
ListMultipartUploadsResponse response = s3.listMultipartUploads(listMultipartUploadsRequest);
List<MultipartUpload> uploads = response.uploads();
for (MultipartUpload upload : uploads) {
System.out.println("Upload in progress: Key = \"" + upload.key() + "\", id = " + upload.uploadId());
}
} catch (S3Exception 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 VPCS3Example method listBucketObjects.
// snippet-start:[s3.java2.vpc.example.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 (S3Object s3Object : objects) {
System.out.print("\n The name of the key is " + s3Object.key());
System.out.print("\n The object is " + convertBToKb(s3Object.size()) + " KBs");
System.out.print("\n The owner is " + s3Object.owner());
}
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
Aggregations