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