AWS SDK Version 3 for .NET
API Reference

AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

The parameters to create a pre-signed URL to a bucket or object.

Inheritance Hierarchy

System.Object
  Amazon.Runtime.AmazonWebServiceRequest
    Amazon.S3.Model.GetPreSignedUrlRequest

Namespace: Amazon.S3.Model
Assembly: AWSSDK.S3.dll
Version: 3.x.y.z

Syntax

C#
public class GetPreSignedUrlRequest : AmazonWebServiceRequest
         IAmazonWebServiceRequest

The GetPreSignedUrlRequest type exposes the following members

Constructors

NameDescription
Public Method GetPreSignedUrlRequest()

Properties

NameTypeDescription
Public Property BucketName System.String

The name of the bucket to create a pre-signed url to, or containing the object.

Public Property ContentType System.String

A standard MIME type describing the format of the object data.

Public Property Expires System.DateTime

The expiry date and time for the pre-signed url.

Public Property Headers Amazon.S3.Model.HeadersCollection

The collection of headers for the request.

Public Property Key System.String

The key to the object for which a pre-signed url should be created.

Public Property Metadata Amazon.S3.Model.MetadataCollection

The collection of meta data for the request.

Public Property Parameters Amazon.S3.Model.ParameterCollection

Custom parameters to include in the signed request, so that they are tamper-proof.

Public Property PartNumber System.Int32

The part number for the multipart upload for which a pre-signed url should be created.

Public Property Protocol Amazon.S3.Protocol

The requested protocol (http/https) for the pre-signed url.

Public Property RequestPayer Amazon.S3.RequestPayer

Confirms that the requester knows that she or he will be charged for the request. Bucket owners need not specify this parameter in their requests.

Public Property ResponseHeaderOverrides Amazon.S3.Model.ResponseHeaderOverrides

A set of response headers that should be returned with the pre-signed url creation response.

Public Property ServerSideEncryptionCustomerMethod Amazon.S3.ServerSideEncryptionCustomerMethod

The Server-side encryption algorithm to be used with the customer provided key.

Public Property ServerSideEncryptionKeyManagementServiceKeyId System.String

The id of the AWS Key Management Service key that Amazon S3 should use to encrypt and decrypt the object. If a key id is not specified, the default key will be used for encryption and decryption.

Public Property ServerSideEncryptionMethod Amazon.S3.ServerSideEncryptionMethod

Specifies the encryption used on the server to store the content.

Public Property UploadId System.String

The upload id for the multipart upload for which a pre-signed url should be created.

Public Property Verb Amazon.S3.HttpVerb

The verb for the pre-signed url.

Public Property VersionId System.String

Version id for the object that the pre-signed url will reference. If not set, the url will reference the latest version of the object.

Methods

Note:

Asynchronous operations (methods ending with Async) in the table below are for .NET 4.5 or higher. For .NET 3.5 the SDK follows the standard naming convention of BeginMethodName and EndMethodName to indicate asynchronous operations - these method pairs are not shown in the table below.

NameDescription
Public Method IsSetExpires()

Checks if Expires property is set.

Examples

The following examples show how to create various different pre-signed URLs.

The code sample shows a GetContents function. This will be referred to in subsequent samples to test out the generated URL.

GetContents function


public static string GetContents(string path)
{
    HttpWebRequest request = HttpWebRequest.Create(path) as HttpWebRequest;
    HttpWebResponse response = request.GetResponse() as HttpWebResponse; 

    using (Stream stream = response.GetResponseStream())
    using (StreamReader reader = new StreamReader(stream))
    {
        return reader.ReadToEnd();
    }
}
        
        

The first example creates a URL that will allow a third party to retrieve an object from S3 for a period of 5 minutes.

GetPreSignedURL sample 1


// Create a client
AmazonS3Client client = new AmazonS3Client();

// Create a CopyObject request
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
    BucketName = "SampleBucket",
    Key = "Item1",
    Expires = DateTime.UtcNow.AddMinutes(5)
};

// Get path for request
string path = client.GetPreSignedURL(request);

// Test by getting contents
string contents = GetContents(path);

                

The following example creates a URL that will allow a third party to retrieve an object from S3 for a period of 5 minutes, and it also sets the headers to specific content, caching and encoding values.

GetPreSignedURL sample 2


// Create a client
AmazonS3Client client = new AmazonS3Client();

// Create a CopyObject request
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
    BucketName = "SampleBucket",
    Key = "Item1",
    Expires = DateTime.UtcNow.AddMinutes(5)
};
request.ResponseHeaderOverrides.ContentType = "text/xml+zip";
request.ResponseHeaderOverrides.ContentDisposition = "attachment; filename=dispName.pdf";
request.ResponseHeaderOverrides.CacheControl = "No-cache";
request.ResponseHeaderOverrides.ContentLanguage = "mi, en";
request.ResponseHeaderOverrides.Expires = "Thu, 01 Dec 1994 16:00:00 GMT";
request.ResponseHeaderOverrides.ContentEncoding = "x-gzip";

// Get path for request
string path = client.GetPreSignedURL(request);

// Test by getting contents
string contents = GetContents(path);

                

This example creates a URL that will allow a third party to list all objects in a specific bucket. This URL will also expire in 5 minutes.
The URL response will be the XML response for a ListBucket request.

GetPreSignedURL sample 3


// Create a client
AmazonS3Client client = new AmazonS3Client();

// Create a CopyObject request
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
    BucketName = "SampleBucket",
    Expires = DateTime.UtcNow.AddMinutes(5)
};

// Get path for request
string path = client.GetPreSignedURL(request);

// Retrieve objects
string allObjects = GetContents(path);

                

This example creates a URL that will allow a third party to list all of the owner's buckets. This URL will also expire in 5 minutes.
The URL response will be the XML response for a ListBuckets request.

GetPreSignedURL sample 4


// Create a client
AmazonS3Client client = new AmazonS3Client();

// Create a CopyObject request
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
    Expires = DateTime.UtcNow.AddMinutes(5)
};

// Get path for request
string path = client.GetPreSignedURL(request);

// Retrieve buckets
string allBuckets = GetContents(path);

                

This final example creates a URL that allows a third party to put an object, then uses it to upload sample content. The URL is set to expire in 10 days.

GetPreSignedURL sample 5


// Create a client
AmazonS3Client client = new AmazonS3Client();

// Create a CopyObject request
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
    BucketName = "SampleBucket",
    Key = "Item1",
    Verb = HttpVerb.PUT,
    Expires = DateTime.UtcNow.AddDays(10)
};

// Get path for request
string path = client.GetPreSignedURL(request);

// Prepare data
byte[] data = UTF8Encoding.UTF8.GetBytes("Sample text.");

// Configure request
HttpWebRequest httpRequest = WebRequest.Create(path) as HttpWebRequest;
httpRequest.Method = "PUT";
httpRequest.ContentLength = data.Length;

// Write data to stream
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();

// Issue request
HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;

                

Remarks

For more information, refer to: http://docs.amazonwebservices.com/AmazonS3/latest/dev/S3_QSAuth.html.
Required Parameters: BucketName, Expires
Optional Parameters: Key, VersionId, Verb: default is GET

Version Information

.NET:
Supported in: 8.0 and newer, Core 3.1

.NET Standard:
Supported in: 2.0

.NET Framework:
Supported in: 4.5 and newer, 3.5