All Products
Search
Document Center

Object Storage Service:Multipart upload (C# SDK V2)

Last Updated:Aug 06, 2025

OSS provides the multipart upload feature that 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-hangzhou of 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 mapping between regions and endpoints supported by OSS, see OSS regions and endpoints.

  • To implement multipart upload, you must have the oss:PutObject permission. For more information, see Grant custom permissions to a RAM user.

Multipart upload process

To implement multipart upload, perform the following steps:

  1. Initialize a multipart upload task.

    Call the Client.InitiateMultipartUploadAsync method to obtain a globally unique upload ID created by OSS.

  2. Upload parts.

    Call the Client.UploadPartAsync method to upload part data.

    Note
    • For parts that are uploaded by running a multipart upload task with a specific upload ID, the part numbers identify their relative positions in an object. If you upload a part and reuse its part number to upload another part, the new part overwrites the original part.

    • OSS includes the MD5 hash of each uploaded part in the ETag header in the response.

    • OSS calculates the MD5 hash of uploaded data and compares the MD5 hash with the MD5 hash that is calculated by the SDK. If the two hashes are different, OSS returns the InvalidDigest error code.

  3. Complete the multipart upload task.

    After all parts are uploaded, call the Client.CompleteMultipartUploadAsync method to combine these parts into a complete object.

Sample code

The following sample code demonstrates how to split a large local file into multiple parts, upload the parts to a bucket, and then combine these parts into a complete object:

using OSS = AlibabaCloud.OSS.V2;  // Create an alias for Alibaba Cloud OSS SDK to simplify subsequent use

var region = "cn-hangzhou";  // Required. Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou
var endpoint = null as string;  // Optional. Specify the endpoint used to access OSS. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com
var bucket = "you bucket name";  // Required. Specify the bucket name
var key = "your object key";  // Required. Specify the name of the object to upload
var partSize = 512*1024;  // Required. Specify the size of each part to upload. In this example, the size of each part is 512 KB
var filePath = "filePath";  // Required. Specify the path of the file to upload

// Load the default configurations of OSS SDK, which automatically reads credential information (such as AccessKey) from environment variables
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set to use environment variables for credentials used for identity verification (format: OSS_ACCESS_KEY_ID, 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, override the default endpoint 
if(endpoint != null) 
{
    cfg.Endpoint = endpoint;
} 

// Create an OSS client instance using the configuration information
using var client = new OSS.Client(cfg); 

// Initialize 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 information about 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 part information for completing the upload later
    uploadParts.Add(new() { PartNumber = partNumber, ETag = upResult.ETag });
    partNumber++;
}

// Sort by part number
uploadParts.Sort((left, right) => { return (left.PartNumber > right.PartNumber) ? 1 : -1; });

// Complete multipart upload
var cmResult = await client.CompleteMultipartUploadAsync(new()
{
    Bucket = bucket,
    Key = key,
    UploadId = initResult.UploadId,
    CompleteMultipartUpload = new ()
    {
        Parts = uploadParts
    }
});

// Display the upload result
Console.WriteLine("MultipartUpload done");  // Indicate the operation is complete
Console.WriteLine($"StatusCode: {cmResult.StatusCode}");  // HTTP status code
Console.WriteLine($"RequestId: {cmResult.RequestId}");  // RequestId, used for Alibaba Cloud troubleshooting
Console.WriteLine("Response Headers:");  // Response header information
cmResult.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));  // Traverse and print all response headers

References

For the complete sample code for multipart upload, see multipartUpload.cs.