31

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;
    }
}

5 Answers 5

32

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);
} 
2
  • Maybe a little late for OP but please also note that the SDK comes with a range of asynchronous upload and download methods which use the Task Parallel Library. Usage is : await fileTransferUtility.UploadAsync(uploadRequest); Sample code from Amazon here : blogs.aws.amazon.com/net/post/TxPJDGHVNCWHUJ/…
    – JJP
    Jul 22, 2015 at 7:57
  • Hii Daniel, If I study and use the above code will the uploaded file be private. If I paste the object URL will it say access denied?
    – Thomas Raj
    Sep 29, 2022 at 3:13
18

Found it, need to use a TransferUtilityUploadRequest:

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);
        var uploadRequest = new TransferUtilityUploadRequest();
        uploadRequest.FilePath = filePath;
        uploadRequest.BucketName = "my_s3_bucket";
        uploadRequest.Key = toPath;
        uploadRequest.AddHeader("x-amz-acl", "public-read");
        transferUtility.BeginUpload(uploadRequest, callback, null);
    }

    private void uploadComplete(IAsyncResult result)
    { 
        var x = result;
    }
}
2
  • 1
    Just FYI, you need to specify the uploadRequest.BucketName for the upload to work.
    – John Dyer
    Aug 7, 2012 at 18:53
  • 2
    This method of setting the headers has been deprecated after S3 version 1. The TransferUtilityUploadRequest object no longer has a AddHeader method. The correct answer is now danielonthenet's.
    – krillgar
    Jul 24, 2014 at 12:30
5

I have done this before, and in C#, but not with your library (so this may be of limited help).

The idea is to send an ACL header along with the file.

This is one way to do it that should point you in the right direction.

Here's also a link to some relevant AWS docs.

    private const string AWS_ACL_HEADER = "x-amz-acl";

    private static string ToACLString(S3ACLType acl) {
        switch (acl) {
            case S3ACLType.AuthenticatedRead:
                return "authenticated-read";
            case S3ACLType.BucketOwnerFullControl:
                return "bucket-owner-full-control";
            case S3ACLType.BucketOwnerRead:
                return "bucket-owner-read";
            case S3ACLType.Private:
                return "private";
            case S3ACLType.PublicRead:
                return "public-read";
            case S3ACLType.PublicReadWrite:
                return "public-read-write";
            default: return "";
        }
    }

    public void Put(string bucketName, string id, byte[] bytes, string contentType, S3ACLType acl) {
        string uri = String.Format("https://{0}/{1}", BASE_SERVICE_URL, bucketName.ToLower());
        DreamMessage msg = DreamMessage.Ok(MimeType.BINARY, bytes);
        msg.Headers[DreamHeaders.CONTENT_TYPE] = contentType;
        msg.Headers[DreamHeaders.EXPECT] = "100-continue";
        msg.Headers[AWS_ACL_HEADER] = ToACLString(acl);
        Plug s3Client = Plug.New(uri).WithPreHandler(S3AuthenticationHeader);
        s3Client.At(id).Put(msg);
    }
1
  • thx for the help with the headers. I posted my own answer just because its using the standard aws library, which takes advatage of s3 multipart upload, which allows you to upload chunks of the file asynchronously making it much faster
    – MattoTodd
    Oct 1, 2011 at 0:23
2

Consider CannedACL approach, shown in the follwing snippet:

        string filePath = @"Path\Desert.jpg";
        Stream input = new FileStream(filePath, FileMode.Open);
        PutObjectRequest request2 = new PutObjectRequest();
        request2.WithMetaData("title", "the title")
            .WithBucketName(bucketName)
            .WithCannedACL(S3CannedACL.PublicRead)
            .WithKey(keyName)
            .WithInputStream(input);
1
  • 1
    The AWS SDK marks this approach as deprecated and will be removed in v2.0
    – winwaed
    Sep 25, 2013 at 22:07
2

You can use the property CannedACL of Amazon.S3.Model.PutObjectRequest to upload files to S3 as shown below,

 var putObjectRequest = new Amazon.S3.Model.PutObjectRequest
                        {
                            BucketName = bucketName,
                            Key = key,
                            CannedACL = S3CannedACL.PublicReadWrite
                        }; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.