如何在授予公共访问权限的情况下将文件上传到AWS S3?

时间:2018-10-08 23:09:00

标签: android amazon-web-services amazon-s3

我正在使用aws示例代码将文件上传到S3,但是当文件上传时,它们对任何人都没有访问权限,即使将存储桶设为对所有人公开也是如此,唯一的读取方法是手动设置S3控制台上的文件以公开访问。

MyService:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null && intent.getStringExtra(INTENT_KEY_NAME) != null) {
        final String key = intent.getStringExtra(INTENT_KEY_NAME);
        final File file = (File) intent.getSerializableExtra(INTENT_FILE);
        final String transferOperation = intent.getStringExtra(INTENT_TRANSFER_OPERATION);
        TransferObserver transferObserver;

        switch (transferOperation) {
            case TRANSFER_OPERATION_DOWNLOAD:
                Log.d(TAG, "Downloading " + key);
                transferObserver = transferUtility.download("aws-MYBUCKET", key, file);
                transferObserver.setTransferListener(new DownloadListener());
                break;
            case TRANSFER_OPERATION_UPLOAD:
                Log.d(TAG, "Uploading " + key);
                transferObserver = transferUtility.upload("aws-MYBUCKET", key, file);
                transferObserver.setTransferListener(new UploadListener());
                break;
        }
        return START_STICKY;
    } else return START_NOT_STICKY;

1 个答案:

答案 0 :(得分:3)

您应该创建一个桶策略。这样可以授予整个存储桶或部分存储桶的公共访问权限。

来自Bucket Policy Examples - Amazon Simple Storage Service

{
  "Version":"2012-10-17",
  "Statement":[
    {
      "Sid":"AddPerm",
      "Effect":"Allow",
      "Principal": "*",
      "Action":["s3:GetObject"],
      "Resource":["arn:aws:s3:::examplebucket/*"]
    }
  ]
}

这比授予对单个对象的访问权限要好。