use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class SetAcl method setBucketAcl.
// snippet-start:[s3.java2.set_acl.main]
public static void setBucketAcl(S3Client s3, String bucketName, String id) {
try {
Grant ownerGrant = Grant.builder().grantee(builder -> {
builder.id(id).type(Type.CANONICAL_USER);
}).permission(Permission.FULL_CONTROL).build();
List<Grant> grantList2 = new ArrayList<>();
grantList2.add(ownerGrant);
AccessControlPolicy acl = AccessControlPolicy.builder().owner(builder -> builder.id(id)).grants(grantList2).build();
PutBucketAclRequest putAclReq = PutBucketAclRequest.builder().bucket(bucketName).accessControlPolicy(acl).build();
s3.putBucketAcl(putAclReq);
} catch (S3Exception e) {
e.printStackTrace();
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class SetBucketPolicy method setPolicy.
// snippet-start:[s3.java2.set_bucket_policy.main]
public static void setPolicy(S3Client s3, String bucketName, String policyText) {
System.out.println("Setting policy:");
System.out.println("----");
System.out.println(policyText);
System.out.println("----");
System.out.format("On Amazon S3 bucket: \"%s\"\n", bucketName);
try {
PutBucketPolicyRequest policyReq = PutBucketPolicyRequest.builder().bucket(bucketName).policy(policyText).build();
s3.putBucketPolicy(policyReq);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
System.out.println("Done!");
}
use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class ManagingObjectTags method putS3ObjectTags.
// snippet-start:[s3.java2.s3_object_manage_tags.main]
public static void putS3ObjectTags(S3Client s3, String bucketName, String objectKey, String objectPath) {
try {
// Define the tags.
Tag tag1 = Tag.builder().key("Tag 1").value("This is tag 1").build();
Tag tag2 = Tag.builder().key("Tag 2").value("This is tag 2").build();
List<Tag> tags = new ArrayList<Tag>();
tags.add(tag1);
tags.add(tag2);
Tagging allTags = Tagging.builder().tagSet(tags).build();
PutObjectRequest putOb = PutObjectRequest.builder().bucket(bucketName).key(objectKey).tagging(allTags).build();
s3.putObject(putOb, RequestBody.fromBytes(getObjectFile(objectPath)));
} 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 PutBucketLogging method setlogRequest.
// snippet-start:[s3.java2.s3_put_log.main]
public static void setlogRequest(S3Client s3, String bucketName, String targetBucket) {
try {
GetBucketAclRequest aclRequest = GetBucketAclRequest.builder().bucket(targetBucket).build();
GetBucketAclResponse response = s3.getBucketAcl(aclRequest);
// Get the Id
String id = response.owner().id();
// Create a Grantee object
Grantee grantee = Grantee.builder().type(Type.GROUP).uri("http://acs.amazonaws.com/groups/s3/LogDelivery").build();
TargetGrant targetGrant = TargetGrant.builder().grantee(grantee).permission(BucketLogsPermission.FULL_CONTROL).build();
List granteeList = new ArrayList<Grantee>();
granteeList.add(targetGrant);
LoggingEnabled loggingEnabled = LoggingEnabled.builder().targetBucket(targetBucket).targetGrants(granteeList).build();
BucketLoggingStatus loggingStatus = BucketLoggingStatus.builder().loggingEnabled(loggingEnabled).build();
PutBucketLoggingRequest loggingRequest = PutBucketLoggingRequest.builder().bucket(bucketName).expectedBucketOwner("814548047983").bucketLoggingStatus(loggingStatus).build();
s3.putBucketLogging(loggingRequest);
System.out.println("Enabling logging for the target bucket " + targetBucket);
} 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 PutObjectMetadata method putS3Object.
// snippet-start:[s3.java2.s3_object_upload.metadata.main]
public static String putS3Object(S3Client s3, String bucketName, String objectKey, String objectPath) {
try {
// Define the metadata
Map<String, String> metadata = new HashMap<>();
metadata.put("author", "Mary Doe");
metadata.put("version", "1.0.0.0");
PutObjectRequest putOb = PutObjectRequest.builder().bucket(bucketName).key(objectKey).metadata(metadata).build();
PutObjectResponse response = s3.putObject(putOb, RequestBody.fromBytes(getObjectFile(objectPath)));
return response.eTag();
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
Aggregations