Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload files to AWS S3 with public access granted?

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;
like image 619
Amy Avatar asked Oct 08 '18 23:10

Amy


People also ask

How do I upload files to AWS S3?

In the Amazon S3 console, choose the bucket where you want to upload an object, choose Upload, and then choose Add Files. In the file selection dialog box, find the file that you want to upload, choose it, choose Open, and then choose Start Upload. You can watch the progress of the upload in the Transfer pane.

What does public access to S3 mean?

S3 Block Public Access provides controls across an entire AWS Account or at the individual S3 bucket level to ensure that objects never have public access, now and in the future. Public access is granted to buckets and objects through access control lists (ACLs), bucket policies, or both.


1 Answers

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.

like image 88
John Rotenstein Avatar answered Sep 30 '22 13:09

John Rotenstein