All Products
Search
Document Center

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

Last Updated:Aug 06, 2025

Append upload lets you append content to an existing appendable object. This topic describes how to perform an append upload using the OSS C# SDK.

Precautions

  • The sample code in this topic uses the China (Hangzhou) region (cn-hangzhou) as an example. By default, the public endpoint is used. 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 that OSS supports, see OSS regions and endpoints.

  • If the object does not exist, the append upload operation creates an appendable object.

  • If the object exists:

    • If the object is an appendable object and the specified append position is the same as the current length of the object, the content is appended to the end of the object.

    • If the object is an appendable object but the specified append position is not the same as the current length of the object, the PositionNotEqualToLength exception is thrown.

    • If the object is not an appendable object, for example, a normal object uploaded using simple upload, the ObjectNotAppendable exception is thrown.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket Policy.

API

Action

Definition

AppendObject

oss:PutObject

You can call this operation to upload an object by appending the object to an existing object.

oss:PutObjectTagging

When uploading an object by appending the object to an existing object, if you specify object tags through x-oss-tagging, this permission is required.

Sample code

using System.Text; // Import the System.Text namespace to process character encoding, such as UTF-8 encoded strings.
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 where the bucket is located. This topic uses China (Hangzhou) as an example. Set the region to cn-hangzhou.
var bucket = "your bucket name";  // Required. Specify the name of the destination bucket. 
var endpoint = null as string;  // Optional. Specify the endpoint that is used to access OSS. This topic uses China (Hangzhou) as an example. Set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var key = "your object key"; // Required. Specify the name of the object to which you want to append data. Format: folder/objectName.

// Load the default configurations of OSS SDK. The configurations automatically read credential information, such as AccessKeys, from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly specify that environment variables are used to obtain credentials for identity verification. Format: OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket 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 information.
using var client = new OSS.Client(cfg);

// Define the first part of the content to be appended to the OSS object.
var content1 = "hello,";
// Define the second part of the content to be appended to the OSS object.
var content2 = "oss!";

// Call the AppendObjectAsync method to perform the first append operation.
var result1 = await client.AppendObjectAsync(new OSS.Models.AppendObjectRequest()
{
    // Specify the destination bucket.
    Bucket = bucket,
    // Specify the name of the destination object. 
    Key = key,
    // For the first append operation, the start position must be 0.
    Position = 0,
    Body = new MemoryStream(Encoding.UTF8.GetBytes(content1))
 });

// Call the AppendObjectAsync method to perform the second append operation, starting from the end position of the previous operation.
var result2 = await client.AppendObjectAsync(new OSS.Models.AppendObjectRequest()
{
     // The destination bucket remains unchanged.
    Bucket = bucket,
    // The name of the destination object remains unchanged.
    Key = key,
    // Continue writing from the end position of the previous append operation.
    Position = result1.NextAppendPosition,
    Body = new MemoryStream(Encoding.UTF8.GetBytes(content2))
});

// Print the upload result.
Console.WriteLine("AppendObject done"); // A message indicates that the operation is complete.
Console.WriteLine($"StatusCode: {result2.StatusCode}");  // The HTTP status code.
Console.WriteLine($"RequestId: {result2.RequestId}");   // The request ID, which is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:"); // The response header information.
result2.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));   // Traverse and print all response headers.
Console.WriteLine($"NextAppendPosition: {result2.NextAppendPosition}"); // Output the start position for the next append operation.

References

For the complete sample code for an append upload, see AppendObject.cs.