Search code examples
androidamazon-web-servicesamazon-s3awss3transferutility

how to set ACL for an object while uploading it to Amazon s3?


i can upload images from Android to Amazon s3. But they are always private, no one can display them. i have to make it public on Amazon s3 management console. i can not set ACL for an image while uploading it.

I learned that, there are two ways to upload image to Amazon s3 from Android:

  • using PutObjectRequest
  • using TransferUtility.upload()

i tried both. if i prefer PutObjectRequest, i have to use AsyncTask or another thread. Because PutObjectRequest runs on main thread. But i do not want to use AsyncTask or another thread.

i want to use TransferUtility, because it does not run on main thread, works on service, so i do not have to use AsyncTask or another thread. Also i can track progress and state of transfer easily. In addition, TransferUtility is newer than PubObjectRequest. But the problem comes here: i do not know how to set ACL for an object while or before uploading it to s3. i tried that :

s3Client.setObjectAcl("myBucket", "steve_jobs.jpg", CannedAccessControlList.PublicRead);

but it returned "network on main thread" error. So i need to use AsyncTask or another thread.but i do not want to use AsyncTask or another thread. how can i set ACL for an object while uploading it with TransferUtility?


Solution

  • Bucket policy seems to be the simpliest thing. You can specify some path or give permission for all objects in you bucket. You can set it using AWS console (Bucket properties -> Permissions -> Edit bucket policy) or CloudBerry Explorer (Bucket properties -> Bucket Policy). Is it suit for your need?

    {
      "Version": "2008-10-17",
      "Statement": [
        {
          "Sid": "",
          "Effect": "Allow",
          "Principal": {
            "AWS": "*"
          },
          "Action": "s3:GetObject",
          "Resource": "arn:aws:s3:::bucketname/*",
          "Condition": {}
        }
      ]
    }