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.
Create a signed URL allowing access to a resource that would usually require authentication.
Namespace: Amazon.S3
Assembly: AWSSDK.S3.dll
Version: 3.x.y.z
public abstract String GetPreSignedURL( GetPreSignedUrlRequest request )
The GetPreSignedUrlRequest that defines the parameters of the operation.
| Exception | Condition |
|---|---|
| System.ArgumentException | |
| System.ArgumentNullException |
When using query string authentication you create a query, specify an expiration time for the query, sign it with your signature, place the data in an HTTP request, and distribute the request to a user or embed the request in a web page.
A PreSigned URL can be generated for GET, PUT, DELETE and HEAD operations on your bucketName, keys, and versions.
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.
public static async Task<string> GetContents(string path)
{
using var httpClient = new HttpClient();
return await httpClient.GetStringAsync(path);
}
The first example creates a URL that will allow a third party to retrieve an object from S3 for a period of 5 minutes.
// Create a client
AmazonS3Client client = new AmazonS3Client();
// Create a CopyObject request
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = "amzn-s3-demo-bucket",
Key = "Item1",
Expires = DateTime.UtcNow.AddMinutes(5)
};
// Get path for request
string path = await client.GetPreSignedURLAsync(request);
// Test by getting contents
string contents = await 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.
// Create a client
AmazonS3Client client = new AmazonS3Client();
// Create a CopyObject request
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = "amzn-s3-demo-bucket",
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 = await client.GetPreSignedURLAsync(request);
// Test by getting contents
string contents = await 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.
// Create a client
AmazonS3Client client = new AmazonS3Client();
// Create a CopyObject request
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = "amzn-s3-demo-bucket",
Expires = DateTime.UtcNow.AddMinutes(5)
};
// Get path for request
string path = await client.GetPreSignedURLAsync(request);
// Retrieve objects
string allObjects = await 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.
// 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 = await client.GetPreSignedURLAsync(request);
// Retrieve buckets
string allBuckets = await 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.
// Create a client
AmazonS3Client client = new AmazonS3Client();
// Create a CopyObject request
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = "amzn-s3-demo-bucket",
Key = "Item1",
Verb = HttpVerb.PUT,
Expires = DateTime.UtcNow.AddDays(10)
};
// Get path for request
string path = await client.GetPreSignedURLAsync(request);
// Prepare data
byte[] data = UTF8Encoding.UTF8.GetBytes("Sample text.");
using var httpClient = new HttpClient();
using var httpRequest = new HttpRequestMessage(HttpMethod.Put, path)
{
Content = new ByteArrayContent(data)
};
using var httpResponse = await httpClient.SendAsync(httpRequest);
.NET:
Supported in: 8.0 and newer, Core 3.1
.NET Standard:
Supported in: 2.0
.NET Framework:
Supported in: 4.7.2 and newer