Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using S3 Storage with .NET

I am using AWS.Net to upload user content (images) and display them on my site. This is what my code looks like currently for the upload:

using (client = Amazon.AWSClientFactory.CreateAmazonS3Client())
    {
        var putObjectRequest = new PutObjectRequest
        {

            BucketName = bucketName,
            InputStream = fileStream,
            Key = fileName,
            CannedACL = S3CannedACL.PublicRead,
            //MD5Digest = md5Base64,
            //GenerateMD5Digest = true,
            Timeout = 3600000 //1 Hour
        };

        S3Response response = client.PutObject(putObjectRequest);
        response.Dispose();
}

Whats the best way to store the path to these files? Is there a way to get a link to my file from the response?

Currently I just have a URL in my webconfig like https://s3.amazonaws.com/<MyBucketName>/ and then when I need to show an image I take that string and use the key from the object I store in the db that represents the file uploaded. Is there a better way to do this?

All the examples that come with it don't really address this kind of usage. And the documentation isn't online and I don't know how to get to it after I install the SDK, despite following the directions on Amazon.

like image 496
Jason Avatar asked Nov 05 '22 09:11

Jason


1 Answers

Your approach of storing paths including your bucket names in web.config is the same thing that I do, which works great. I then just store the relative paths in various database tables.

The nice thing about this approach is that it makes it easier to migrate to different storage mechanisms or CDNs such as CloudFront. I don't think that there's a better way than this approach because S3 files reside on a different domain, or subdomain if you do CNAME mapping, and thus your .NET runtime does not run under the same domain or subdomain.

like image 160
Shan Plourde Avatar answered Nov 14 '22 22:11

Shan Plourde