Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make an S3 object public via the aws Java SDK?

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?

like image 392
MikeNereson Avatar asked Jun 29 '11 16:06

MikeNereson


People also ask

How do I make my S3 bucket from private to public?

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.

How do I access an S3 bucket from AWS SDK?

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.

How you can access S3 bucket through Java program?

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.


2 Answers

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)

like image 81
MikeNereson Avatar answered Oct 02 '22 18:10

MikeNereson


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); 
like image 20
Maurice Avatar answered Oct 02 '22 18:10

Maurice