Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

使用预签名 URL 上传对象 (适用于 .NET 的 AWS 开发工具包)

以下任务将引导您使用 .NET 类上传使用预签名 URL 的对象。

上传对象

1

创建 AmazonS3 类的实例。

生成预签名 URL 时,这些凭证将用于为身份验证创建一个签名。

2

通过执行 AmazonS3.GetPreSignedURL 方法生成预签名 URL。

您可以通过创建 GetPreSignedUrlRequest 类的实例来提供存储桶名称、对象键和过期日期。如果您计划使用此 URL 来上传对象,您必须在创建它时指定 HTTP 动作 PUT。

3

任何拥有预签名 URL 的人都可以上传对象。您可以通过提供预签名的 URL 和上传对象,创建 HttpWebRequest 类的实例。

以下 C# 代码示例演示了上述任务。

Copy
IAmazonS3 client; client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); // Generate a pre-signed URL. GetPreSignedUrlRequest request = new GetPreSignedUrlRequest { BucketName = bucketName, Key = objectKey, Verb = HttpVerb.PUT, Expires = 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;

以下 C# 代码示例为特定的对象生成了预签名 URL 并使用它来上传文件。有关如何创建和测试有效示例的说明,请参阅 运行 Amazon S3 .NET 代码示例

Copy
using System; using System.IO; using System.Net; using Amazon.S3; using Amazon.S3.Model; namespace s3.amazon.com.docsamples { class UploadObjectUsingPresignedURL { static IAmazonS3 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) { try { using (s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { 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 { BucketName = bucketName, Key = objectKey, Verb = HttpVerb.PUT, Expires = DateTime.Now.AddMinutes(5) }; string url = null; url = s3Client.GetPreSignedURL(request); return url; } } }