How do you make an S3 object public via the AWS Java SDK?
Specifically, what API methods via the Java AWS SDK can be used to make an Object public when its uploaded?
Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Bucket name list, choose the name of the bucket that you want. Choose Permissions. Choose Edit to change the public access settings for the bucket.
To access Amazon Simple Storage Service, create an AWS. S3 service object. Call the listBuckets method of the Amazon S3 service object to retrieve a list of your buckets. The data parameter of the callback function has a Buckets property containing an array of maps to represent the buckets.
We can get these credentials in two ways, either by using AWS root account credentials from the access keys section of the Security Credentials page, or by using IAM user credentials from the IAM console. Choosing AWS Region: We also have to select the AWS region(s) where we want to store our Amazon S3 data.
Found the answer in an amazon aws forum.
return s3Client.putObject( new PutObjectRequest(bucketName, objectKey, inputStream, metadata) .withCannedAcl(CannedAccessControlList.PublicRead));
The answer being
.withCannedAcl(CannedAccessControlList.PublicRead)
An alternative approach which allows for more finegrained control of who exactly is allowed to view the object (all users or authenticated users only):
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, image); AccessControlList acl = new AccessControlList(); acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); //all users or authenticated putObjectRequest.setAccessControlList(acl); s3client.putObject(putObjectRequest);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With