7
votes

I'm using the aws sample code to upload files to the S3, but when they get uploaded they come with no access for anyone, even with the bucket made Public for everyone, the only way to read it is to manually set the files on the S3 console to give public access.

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

1 Answers

14
votes

You should create a Bucket Policy. This can grant public access for the whole bucket, or a portion of the bucket.

From 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/*"]
    }
  ]
}

This is preferable to granting access on individual objects.