Question

I am creating files in an S3 bucket using AWS SDK for .Net, I'm using S3FileInfo, everything is working fine but the files are created private, and I cannot seem to find a way to create them public.

Was it helpful?

Solution

"By default your Amazon S3 buckets and objects are private. To make an object viewable by using a URL, for example https://s3.amazonaws.com/Bucket/Object, you must make the object publicly readable. Otherwise, you will need to create a signed URL that includes a signature with authentication information."

You can make an object publicly readable by adding an entry to the Access Control List. It can done via PutACL() method in AmazonS3Client class.

Here's the working example:

static void Main()
{
    // data
    const string accessKeyId = "AKIAIR42XOXH7RXDLVTA";
    const string secretAccessKey = "z4cODfNLYU0YGf7MnXMCXGib2QaxM+inbA1UZw3k";
    const string testBucket = "qwe.SampleBucket";
    const string testFile = "testFile.txt";

    using (var s3Client = new AmazonS3Client(accessKeyId, secretAccessKey, RegionEndpoint.USEast1))
    {
        // creating a file
        var s3FileInfo = new S3FileInfo(s3Client, testBucket, testFile);
        using (var streamWriter = s3FileInfo.CreateText())
        {
            streamWriter.Write("TestContent");
        }

        // setting a permission
        s3Client.PutACL(new PutACLRequest
        {
            BucketName = testBucket,
            Key = testFile,
            CannedACL = S3CannedACL.PublicRead
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top