Object Storage Service (OSS) provides the multipart upload feature. This feature lets you split a large object into multiple parts and upload them separately. After all parts are uploaded, you can call the CompleteMultipartUploadAsync operation to combine these parts into a complete object.
Notes
-
The sample code in this topic uses the region ID
cn-hangzhouof the China (Hangzhou) region as an example and uses the public endpoint by default. If you want to access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints. -
To perform a multipart upload, you must have the
oss:PutObjectpermission. For more information, see Grant custom permissions to a RAM user.
Multipart upload process
A multipart upload consists of three steps:
-
Initialize a multipart upload task.
You can call the Client.InitiateMultipartUploadAsync method to obtain a globally unique upload ID from OSS.
-
Upload parts.
You can call the Client.UploadPartAsync method to upload the part data.
Note-
For the same upload ID, the part number identifies the relative position of the part within the complete object. If you upload new data using the same part number, the existing data for that part on OSS is overwritten.
-
OSS includes the MD5 hash of the uploaded part data in the ETag header of the response.
-
OSS calculates the MD5 hash of the uploaded data and compares it with the MD5 hash calculated by the software development kit (SDK). If the two hashes do not match, OSS returns the InvalidDigest error code.
-
-
Complete the multipart upload task.
After all parts are uploaded, you can call the Client.CompleteMultipartUploadAsync method to combine all parts into a complete object.
Sample code
The following code provides an example of how to split a large local file into multiple parts, upload the parts to a bucket concurrently, and then combine the parts into a complete object.
using OSS = AlibabaCloud.OSS.V2; // Create an alias for the Alibaba Cloud OSS SDK to simplify its use.
var region = "cn-hangzhou"; // Required. The region where the bucket is located. This example uses China (Hangzhou). Set Region to cn-hangzhou.
var endpoint = null as string; // Optional. The domain name to access OSS. This example uses China (Hangzhou). Set Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "you bucket name"; // Required. The bucket name.
var key = "your object key"; // Required. The name of the object to upload.
var partSize = 512*1024; // Required. The size of each part. This example uses 512 * 1024, which is 512 KB.
var filePath = "filePath"; // Required. The path of the file to upload.
// Load the default configurations of the OSS SDK. These configurations automatically read credentials, such as the AccessKey, from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly configure the client to obtain credentials from environment variables for identity verification. The format is OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the bucket region in the configuration.
cfg.Region = region;
// If an endpoint is specified, it overwrites the default endpoint.
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance using the configuration.
using var client = new OSS.Client(cfg);
// Initialize the multipart upload.
var initResult = await client.InitiateMultipartUploadAsync(new()
{
Bucket = bucket,
Key = key
});
// Open the file to upload.
using var file = File.OpenRead(filePath);
long fileSize = file.Length;
long partNumber = 1;
// Store the information of all uploaded parts.
var uploadParts = new List<OSS.Models.UploadPart>();
// Upload the file in parts.
for (long offset = 0; offset < fileSize; offset += partSize)
{
// Calculate the size of the current part.
var size = Math.Min(partSize, fileSize - offset);
// Upload a single part.
var upResult = await client.UploadPartAsync(new()
{
Bucket = bucket,
Key = key,
PartNumber = partNumber,
UploadId = initResult.UploadId,
Body = new OSS.IO.BoundedStream(file, offset, size)
});
// Save the part information to complete the upload later.
uploadParts.Add(new() { PartNumber = partNumber, ETag = upResult.ETag });
partNumber++;
}
// Sort the parts by part number.
uploadParts.Sort((left, right) => { return (left.PartNumber > right.PartNumber) ? 1 : -1; });
// Complete the multipart upload.
var cmResult = await client.CompleteMultipartUploadAsync(new()
{
Bucket = bucket,
Key = key,
UploadId = initResult.UploadId,
CompleteMultipartUpload = new ()
{
Parts = uploadParts
}
});
// Print the upload result.
Console.WriteLine("MultipartUpload done"); // Indicate that the operation is complete.
Console.WriteLine($"StatusCode: {cmResult.StatusCode}"); // The HTTP status code.
Console.WriteLine($"RequestId: {cmResult.RequestId}"); // The request ID, which is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:"); // The response headers.
cmResult.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headers.
Related documents
For the complete sample code for multipart upload, see multipartUpload.cs.