C# – Amazon S3 – How to properly build URLs pointing to the objects in a bucket

amazon-s3c++url

I'm using the Amazon S3 environment to store images for a C# Webapplication. From the S3 documentation I learned, that the basic URL to access an object looks like

http://[bucket-name].S3.amazonaws.com/[key]

I know that one can build expiring URLs to the objects in a bucket. Two questions:

  1. Should one generally use expiring URL's?
  2. How would I build an expiring url?

Best Solution

You only need to build expiring urls if you want to restrict access.

Here's some code to generate the signed url which expires in 3 mins.

using (var s3Client = AWSClientFactory.CreateAmazonS3Client("MyAccessKey", "MySecretKey"))
{
    GetPreSignedUrlRequest request = new GetPreSignedUrlRequest()
        .WithBucketName("MyBucketName")
        .WithKey("MyFileKey")
        .WithProtocol(Protocol.HTTP)
        .WithExpires(DateTime.Now.AddMinutes(3));

    string url = s3Client.GetPreSignedURL(request);
}