| Did this page help you? Yes No Tell us about it... |
The following tasks guide you through using the low-level .NET classes to upload a file.
Low-Level API File UploadingProcess
1 | Create an instance of the |
2 | Initiate multipart upload by executing the
|
3 | Save the Upload ID that the |
4 | Upload the parts. For each part upload, execute the
|
5 | Save the response of the |
6 | Repeat tasks 4 and 5 for each part. |
7 | Execute the AmazonS3Client.CompleteMultipartUpload method to
complete the multipart upload. |
The following C# code sample demonstrates the preceding tasks.
AmazonS3 s3Client = new AmazonS3Client(AccessKeyID, SecretAccessKey);
// List to store upload part responses.
List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>();
// 1. Initialize.
InitiateMultipartUploadRequest initRequest =
new InitiateMultipartUploadRequest()
.WithBucketName(existingBucketName)
.WithKey(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()
.WithBucketName(existingBucketName)
.WithKey(keyName)
.WithUploadId(initResponse.UploadId)
.WithPartNumber(i)
.WithPartSize(partSize)
.WithFilePosition(filePosition)
.WithFilePath(filePath);
// Upload part and add response to our list.
uploadResponses.Add(s3Client.UploadPart(uploadRequest));
filePosition += partSize;
}
// Step 3: complete.
CompleteMultipartUploadRequest compRequest =
new CompleteMultipartUploadRequest()
.WithBucketName(existingBucketName)
.WithKey(keyName)
.WithUploadId(initResponse.UploadId)
.WithPartETags(uploadResponses);
CompleteMultipartUploadResponse completeUploadResponse =
s3Client.CompleteMultipartUpload(compRequest);
}
catch (Exception exception)
{
Console.WriteLine("Exception occurred: {0}", exception.Message);
s3Client.AbortMultipartUpload(new AbortMultipartUploadRequest()
.WithBucketName(existingBucketName)
.WithKey(keyName)
.WithUploadId(initResponse.UploadId));
}![]() | Note |
|---|---|
When uploading large objects using the .NET API, timeout might occur even while data is
being written to the request stream. You can set explicit timeout using the
|
Example
The following C# code example uploads a file to an Amazon S3 bucket. For instructions on how to create and test a working sample, see Testing the .NET Code Examples
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using Amazon.S3;
using Amazon.S3.Model;
namespace s3.amazon.com.docsamples.LowLevel_UploadFromFile
{
class Program
{
// Your AWS Credentials.
static string AccessKeyID = "";
static string SecretAccessKey = "";
static string existingBucketName = "*** Provide bucket name";
static string keyName = "*** Provide object key ***";
static string filePath = "*** Provide file to upload ***";
static void Main(string[] args)
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
AccessKeyID = appConfig["AWSAccessKey"];
SecretAccessKey = appConfig["AWSSecretKey"];
AmazonS3 s3Client =
new AmazonS3Client(AccessKeyID, SecretAccessKey);
// List to store upload part responses.
List<UploadPartResponse> uploadResponses =
new List<UploadPartResponse>();
// 1. Initialize.
InitiateMultipartUploadRequest initiateRequest =
new InitiateMultipartUploadRequest()
.WithBucketName(existingBucketName)
.WithKey(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++)
{
// Create request to upload a part.
UploadPartRequest uploadRequest = new UploadPartRequest()
.WithBucketName(existingBucketName)
.WithKey(keyName)
.WithUploadId(initResponse.UploadId)
.WithPartNumber(i)
.WithPartSize(partSize)
.WithFilePosition(filePosition)
.WithFilePath(filePath);
// Upload part and add response to our list.
uploadResponses.Add(s3Client.UploadPart(uploadRequest));
filePosition += partSize;
}
// Step 3: complete.
CompleteMultipartUploadRequest completeRequest =
new CompleteMultipartUploadRequest()
.WithBucketName(existingBucketName)
.WithKey(keyName)
.WithUploadId(initResponse.UploadId)
.WithPartETags(uploadResponses);
CompleteMultipartUploadResponse completeUploadResponse =
s3Client.CompleteMultipartUpload(completeRequest);
}
catch (Exception exception)
{
Console.WriteLine("Exception occurred: {0}", exception.Message);
s3Client.AbortMultipartUpload(new AbortMultipartUploadRequest()
.WithBucketName(existingBucketName)
.WithKey(keyName)
.WithUploadId(initResponse.UploadId));
}
}
}
}