Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

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;

    }

}

1 Answer

0 votes
by (44.4k points)

Using TransferUtilityUploadRequest can solve it:

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;

    }

}

Related questions

0 votes
1 answer

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...