Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

上传文件

以下任务将引导您使用低级别的 .NET 类来上传文件。

低级别 API 文件上传过程

1

通过提供您的 AWS 凭证,创建 AmazonS3Client 类的实例。

2

通过执行 AmazonS3Client.InitiateMultipartUpload 方法开始分段上传。您将需要通过创建 InitiateMultipartUploadRequest 类的实例,提供所需的信息来启动分段上传。

3

保存 AmazonS3Client.InitiateMultipartUpload 方法返回的上传 ID。您将需要为随后的每个分段上传操作提供此上传 ID。

4

上传分段。对每个上传分段执行 AmazonS3Client.UploadPart 方法。您将需要提供分段上传信息,例如上传 ID、存储桶名称和分段编号。您可以通过创建 UploadPartRequest 类的实例提供此信息。

5

AmazonS3Client.UploadPart 方法的响应保存在列表中。此响应包括以后完成分段上传时您将需要的 ETag 值和分段编号。

6

为每个分段重复任务 4 和 5。

7

执行 AmazonS3Client.CompleteMultipartUpload 方法来完成分段上传。

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

IAmazonS3 s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); // List to store upload part responses. List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>(); // 1. Initialize. InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest { BucketName = existingBucketName, Key = keyName }; InitiateMultipartUploadResponse initResponse = s3Client.InitiateMultipartUpload(initRequest); // 2. Upload Parts. long contentLength = new FileInfo(filePath).Length; long partSize = 5242880; // 5 MB try { long filePosition = 0; for (int i = 1; filePosition < contentLength; i++) { // Create request to upload a part. UploadPartRequest uploadRequest = new UploadPartRequest { BucketName = existingBucketName, Key = keyName, UploadId = initResponse.UploadId, PartNumber = i, PartSize = partSize, FilePosition = filePosition, FilePath = filePath }; // Upload part and add response to our list. uploadResponses.Add(s3Client.UploadPart(uploadRequest)); filePosition += partSize; } // Step 3: complete. CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest { BucketName = existingBucketName, Key = keyName, UploadId = initResponse.UploadId, }; CompleteMultipartUploadResponse completeUploadResponse = s3Client.CompleteMultipartUpload(completeRequest); } catch (Exception exception) { Console.WriteLine("Exception occurred: {0}", exception.Message); AbortMultipartUploadRequest abortMPURequest = new AbortMultipartUploadRequest { BucketName = existingBucketName, Key = keyName, UploadId = initResponse.UploadId }; s3Client.AbortMultipartUpload(abortMPURequest); }

注意

使用 .NET API 上传大型对象时,即使数据已写入请求流,仍可能发生超时。您可以使用 UploadPartRequest 设置显式超时。

以下 C# 代码示例将文件上传到 Amazon S3 存储桶。有关如何创建和测试有效示例的说明,请参阅 运行 Amazon S3 .NET 代码示例

using System; using System.Collections.Generic; using System.IO; using Amazon.S3; using Amazon.S3.Model; namespace s3.amazon.com.docsamples { class UploadFileMPULowLevelAPI { static string existingBucketName = "*** bucket name ***"; static string keyName = "*** key name ***"; static string filePath = "*** file path ***"; static void Main(string[] args) { IAmazonS3 s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); // List to store upload part responses. List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>(); // 1. Initialize. InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest { BucketName = existingBucketName, Key = keyName }; InitiateMultipartUploadResponse initResponse = s3Client.InitiateMultipartUpload(initiateRequest); // 2. Upload Parts. long contentLength = new FileInfo(filePath).Length; long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB try { long filePosition = 0; for (int i = 1; filePosition < contentLength; i++) { UploadPartRequest uploadRequest = new UploadPartRequest { BucketName = existingBucketName, Key = keyName, UploadId = initResponse.UploadId, PartNumber = i, PartSize = partSize, FilePosition = filePosition, FilePath = filePath }; // Upload part and add response to our list. uploadResponses.Add(s3Client.UploadPart(uploadRequest)); filePosition += partSize; } // Step 3: complete. CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest { BucketName = existingBucketName, Key = keyName, UploadId = initResponse.UploadId, //PartETags = new List<PartETag>(uploadResponses) }; completeRequest.AddPartETags(uploadResponses); CompleteMultipartUploadResponse completeUploadResponse = s3Client.CompleteMultipartUpload(completeRequest); } catch (Exception exception) { Console.WriteLine("Exception occurred: {0}", exception.Message); AbortMultipartUploadRequest abortMPURequest = new AbortMultipartUploadRequest { BucketName = existingBucketName, Key = keyName, UploadId = initResponse.UploadId }; s3Client.AbortMultipartUpload(abortMPURequest); } } } }