Moon
Moon

Reputation: 133

AWS S3 upload via android : setting the file to be public for default

As the title states I want users to upload images to my AWS S3 bucket

and I want the images to be made public.

Thought being public should be one of the metadatas but actually they're not...

Will there be any way to set uploaded files to be set public for default?

Upvotes: 2

Views: 2702

Answers (1)

filipebarretto
filipebarretto

Reputation: 1952

To set S3 objects public using the Android SDK you can attach a Bucket Policy to your bucket like:

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

With such policy, all objects in the given bucket will be public.

Or, if you don't want to make all objects public in the bucket, just the ones uploaded using your Android SDK, you can use:

AmazonS3 s3 = new AmazonS3Client(credentials);
s3.setObjectAcl(Constants.S3_BUCKET, key,CannedAccessControlList.PublicRead);

final TransferUtility transferUtility = new TransferUtility(s3, getActivity());

TransferObserver observer = transferUtility.upload(
                            Constants.S3_BUCKET,    // S3 BUCKET TO UPLOAD FILE TO
                            key,                    // NAME OF THE FILE IN THE S3 BUCKET
                            file                    // FILE THAT WILL BE UPLOADED
                    );

Some related questions:

Upvotes: 4

Related Questions