Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Generate Pre-signed Object URL using AWS SDK for .NET

The following tasks guide you through using the .NET classes to generate a pre-signed URL.

Downloading Objects

1

Create an instance of the AmazonS3 class by providing your AWS credentials. These credentials are used in creating a signature for authentication when you generate a pre-signed URL.

2

Execute the AmazonS3.GetPreSignedURL method to generate a pre-signed URL.

You provide information including a bucket name, an object key, and an expiration date by creating an instance of the GetPreSignedUrlRequest class.


The following C# code sample demonstrates the preceding tasks.

static AmazonS3 client;
client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                    accessKeyID, secretAccessKeyID);

GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
request.WithBucketName(bucketName);
request.WithKey(objectKey);
request.Verb = HttpVerb.GET; // Default.
request.WithExpires(DateTime.Now.AddMinutes(5));

string url = s3Client.GetPreSignedURL(request);

Example

The following C# code example generates a pre-signed URL for a specific object. For instructions about how to create and test a working sample, see Testing the .NET Code Examples

using System;
using System.Configuration;
using System.Collections.Specialized;
using Amazon.S3;
using Amazon.S3.Model;

namespace s3.amazon.com.docsamples.presignedurl
{
    class S3Sample
    {
        static string bucketName = "*** Provide a bucket name ***"; 
        static string objectKey = "*** Provide an object name ***";
        static AmazonS3 s3Client;

        public static void Main(string[] args)
        {
            NameValueCollection appConfig = ConfigurationManager.AppSettings;

            string accessKeyID = appConfig["AWSAccessKey"];
            string secretAccessKeyID = appConfig["AWSSecretKey"];
               using (s3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                    accessKeyID, secretAccessKeyID))
                {
                        GeneratePreSignedURL();
                }
            

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        static void GeneratePreSignedURL()
        {
            GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
            request.WithBucketName(bucketName);
            request.WithKey(objectKey);
            request.WithExpires(DateTime.Now.AddMinutes(5));

            try
            {
                string url = s3Client.GetPreSignedURL(request);
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                    ||
                    amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Check the provided AWS Credentials.");
                    Console.WriteLine(
                    "To sign up for service, go to http://aws.amazon.com/s3");
                }
                else
                {
                    Console.WriteLine(
                     "Error occurred. Message:'{0}' when listing objects",
                     amazonS3Exception.Message);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        }
    }
}