0

I keep trying to upload a file to an s3 bucket. But the file is never public for viewing. I have to manually make the files/folder in the bucket public (for every upload) for it to be view able.

Is there a way to upload an Android file (bitmap) with the default permission to be public for viewing during. I would prefer to do this programmatically, if possible. I've checked the s3 docs, couldn't find anything helpful.

Ebad Saghar
  • 1,107
  • 2
  • 16
  • 41

2 Answers2

10

Yes, it is very easy for those guys, who is using TransferUtility, use below method for uploading any file for public readable form using TransferUtility in android,

transferUtility.upload(String bucketName,String key,File file,CannedAccessControlList cannedAcl)

Example:

transferUtility.upload("MY_BUCKET_NAME","FileName",your_file,CannedAccessControlList.PublicRead);
varotariya vajsi
  • 3,965
  • 37
  • 39
  • 1
    Wrong 4th argument found. Required ObjectMetaData as 4th argument. – Hardik Joshi Mar 11 '17 at 13:45
  • 2
    Hello @HardikJoshi, i am using following library in my project, - compile 'com.amazonaws:aws-android-sdk-core:2.2.+' - compile 'com.amazonaws:aws-android-sdk-s3:2.2.+' - compile 'com.amazonaws:aws-android-sdk-ddb:2.2.+' and there are four methods in the TransferUtility class for uploading file, and if you are using ObjectMetadata also as a parameter you can pass "new ObjectMetadata()" in the fourth parameter. – varotariya vajsi Mar 14 '17 at 06:27
  • 1
    Ok. after update library to latest version, it works. Thanks bro! – Hardik Joshi Mar 14 '17 at 08:06
5

To make all objects in your bucket public by default, view the answers to this question or this question.

To specify public access for each file when you are uploading them via Android, set the ACL on the PutObjectRequest like this:

PutObjectRequest putObjectRequest = new PutObjectRequest()
    .withCannedAcl(CannedAccessControlList.PublicRead)

It doesn't look like you can set the ACL in one step with TransferUtility at this time: https://github.com/aws/aws-sdk-android/issues/63

So after uploading the file via TransferUtility you would need to do the following:

s3client.setObjectAcl(bucketName, keyName, CannedAccessControlList.PublicRead);
Community
  • 1
  • 1
Mark B
  • 183,023
  • 24
  • 297
  • 295
  • 1
    Could you edit your code and include an example of setting the ACL when uploading via transfer utility service? – Ebad Saghar Mar 24 '16 at 17:52
  • 1
    I also having same problem while i use putObjectRequest than my device getting freeze.Please help me if anyone have solution.for further detail please see my question [here](http://stackoverflow.com/questions/37923689/device-is-freez-while-set-acl-permission-of-s3-bucket). – Chirag Solanki Jun 21 '16 at 05:32