use of software.amazon.awssdk.services.s3.S3Client 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.S3Client in project aws-doc-sdk-examples by awsdocs.
the class SetBucketPolicy method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " <bucketName> <polFile>\n\n" + "Where:\n" + " bucketName - the Amazon S3 bucket to set the policy on.\n" + " polFile - a JSON file containing the policy (see the Amazon S3 Readme for an example). \n";
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String polFile = args[1];
String policyText = getBucketPolicyFromFile(polFile);
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder().region(region).build();
setPolicy(s3, bucketName, policyText);
s3.close();
}
use of software.amazon.awssdk.services.s3.S3Client 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.S3Client in project aws-doc-sdk-examples by awsdocs.
the class PutBucketLogging method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " <bucketName> <targetBucket> \n\n" + "Where:\n" + " bucketName - the Amazon S3 bucket to upload an object into.\n" + " targetBucket - the target bucket .\n";
if (args.length != 3) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String targetBucket = args[1];
Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder().region(region).build();
setlogRequest(s3, bucketName, targetBucket);
s3.close();
}
use of software.amazon.awssdk.services.s3.S3Client 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);
}
}
Aggregations