将对象上传到Amazon s3时如何设置对象的ACL? [英] how to set ACL for an object while uploading it to Amazon s3?

查看:836
本文介绍了将对象上传到Amazon s3时如何设置对象的ACL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将图像从Android上传到Amazon s3.但是它们始终是私有的,没有人可以显示它们.我必须在Amazon s3管理控制台上将其公开.上传图像时,我无法为图像设置ACL.

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.

我了解到,有两种方法可以将图像从Android上传到Amazon s3:

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

  • 使用PutObjectRequest
  • 使用TransferUtility.upload()

我都尝试过.如果我更喜欢PutObjectRequest,则必须使用AsyncTask或其他线程.因为PutObjectRequest在主线程上运行.但是我不想使用AsyncTask或其他线程.

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.

我想使用TransferUtility,因为它不运行在主线程上,可以在服务上运行,所以我不必使用AsyncTask或其他线程.我也可以轻松跟踪进度和转移状态.此外,TransferUtility比PubObjectRequest更新.但是问题来了:我不知道如何在将对象上传到s3时或之前设置对象的ACL.我尝试过了:

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);

,但返回网络在主线程上"错误.所以我需要使用AsyncTask或另一个线程.但是我不想使用AsyncTask或另一个线程.使用TransferUtility上传对象时如何设置对象的ACL?

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?

推荐答案

桶策略似乎是最简单的方法.您可以为存储桶中的所有对象指定一些路径或授予权限. 您可以使用AWS控制台(存储桶属性->权限->编辑存储桶策略)或CloudBerry Explorer(存储桶属性->存储桶策略)进行设置. 是否适合您的需求?

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

这篇关于将对象上传到Amazon s3时如何设置对象的ACL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆