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

蓝咒 提交于 2019-12-01 10:18:00

问题


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?


回答1:


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": {}
    }
  ]
}


来源:https://stackoverflow.com/questions/35380872/how-to-set-acl-for-an-object-while-uploading-it-to-amazon-s3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!