Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 upload with public permissions

Tags:

c#

amazon-s3

I'm using the Amazon C# SDK and trying to upload a file, but by default it has restricted permissions. I would like to make it publicly available, but I can't seem to find out how to do it as part of the upload.

My bucket is public, but when I upload a new file using the code below, the file I upload is not public.

Has anyone had to do this before?

public class S3Uploader {     private string awsAccessKeyId;     private string awsSecretAccessKey;     private string bucketName;     private Amazon.S3.Transfer.TransferUtility transferUtility;      public S3Uploader(string bucketName)     {         this.bucketName = bucketName;         this.transferUtility = new Amazon.S3.Transfer.TransferUtility("XXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");      }      public void UploadFile(string filePath, string toPath)     {         AsyncCallback callback = new AsyncCallback(uploadComplete);         transferUtility.BeginUpload(filePath, bucketName, toPath, callback, null);     }      private void uploadComplete(IAsyncResult result)     {          var x = result;     } } 
like image 504
MattoTodd Avatar asked Sep 30 '11 22:09

MattoTodd


People also ask

Can anyone upload to a public S3 bucket?

Before you can upload files to an Amazon S3 bucket, you need write permissions for the bucket. For more information about access permissions, see Identity and access management in Amazon S3. You can upload any file type—images, backups, data, movies, etc. —into an S3 bucket.

What does it mean if an S3 bucket is public?

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.

Is S3 public or private by default?

By default, all S3 buckets are private and can be accessed only by users who are explicitly granted access. Restrict access to your S3 buckets or objects by doing the following: Writing IAM user policies that specify the users that can access specific buckets and objects.


1 Answers

The solution by Tahbaza is correct, but it doesn't work in the later versions of AWS SDK (2.1+)...

Consider using the following for 2.1+ versions of SDK:

private void UploadFileToS3(string filePath) {     var awsAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"];     var awsSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"];     var existingBucketName = ConfigurationManager.AppSettings["AWSBucketName"];     var client = Amazon.AWSClientFactory.CreateAmazonS3Client(awsAccessKey, awsSecretKey,RegionEndpoint.USEast1);      var uploadRequest = new TransferUtilityUploadRequest     {         FilePath = filePath,         BucketName = existingBucketName,         CannedACL = S3CannedACL.PublicRead     };  var fileTransferUtility = new TransferUtility(client);     fileTransferUtility.Upload(uploadRequest); }  
like image 77
danielonthenet Avatar answered Sep 22 '22 07:09

danielonthenet