| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
The following tasks guide you through using the .NET classes to upload an object using a pre-signed URL.
Uploading Objects
|
1 |
Create an instance of the These credentials are used in creating a signature for authentication when you generate a pre-signed URL. |
|
2 |
Generate a pre-signed URL by executing the
You provide a bucket name, an object key, and an expiration date by creating an
instance of the |
|
3 |
Anyone with the pre-signed URL can upload an object. You can create an instance of
the |
The following C# code sample demonstrates the preceding tasks.
static AmazonS3 client;
client = Amazon.AWSClientFactory.CreateAmazonS3Client(
accessKeyID, secretAccessKeyID);
// Generate a pre-signed URL.
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
request.WithBucketName(bucketName);
request.WithKey(objectKey);
request.Verb = HttpVerb.PUT;
request.WithExpires(DateTime.Now.AddMinutes(5));
string url = null;
url = s3Client.GetPreSignedURL(request);
// Upload a file using the pre-signed URL.
HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;
httpRequest.Method = "PUT";
using (Stream dataStream = httpRequest.GetRequestStream())
{
// Upload object.
}
HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;Example
The following C# code example generates a pre-signed URL for a specific object and uses it to upload a file. 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;
using System.Net;
using System.IO;
namespace s3.amazon.com.docsamples.putobjectusingpresignedurl
{
class S3Sample
{
static AmazonS3 s3Client;
// File to upload.
static string filePath = @"*** Specify file to upload ***";
// Information to generate pre-signed object URL.
static string bucketName = "*** Provide bucket name ***";
static string objectKey = "*** Provide object key for the new object ***";
public static void Main(string[] args)
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
string accessKeyID = appConfig["AWSAccessKey"];
string secretAccessKeyID = appConfig["AWSSecretKey"];
try
{
using (s3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(
accessKeyID, secretAccessKeyID))
{
string url = GeneratePreSignedURL();
UploadObject(url);
}
}
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);
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
static void UploadObject(string url)
{
HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;
httpRequest.Method = "PUT";
using (Stream dataStream = httpRequest.GetRequestStream())
{
byte[] buffer = new byte[8000];
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
dataStream.Write(buffer, 0, bytesRead);
}
}
}
HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;
}
static string GeneratePreSignedURL()
{
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
request.WithBucketName(bucketName);
request.WithKey(objectKey);
request.Verb = HttpVerb.PUT;
request.WithExpires(DateTime.Now.AddMinutes(5));
string url = null;
url = s3Client.GetPreSignedURL(request);
return url;
}
}
}