All Products
Search
Document Center

Object Storage Service:Synchronous processing (OSS SDK for C# V2)

Last Updated:Jun 17, 2026

Synchronous processing (x-oss-process) requires a program to wait for a task to complete before proceeding. You can use OSS SDK for C# V2 to perform synchronous image processing.

Precautions

The sample code uses the China (Hangzhou) region (cn-hangzhou) and the public endpoint. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about supported regions and endpoints, see Regions and endpoints.

Sample code

The following example shows how to scale an image and save the processed image to a specified bucket.

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

var region = "cn-hangzhou";  // Required. The region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set this parameter to cn-hangzhou.
var endpoint = null as string;  // Optional. The endpoint used to access OSS. For example, if the bucket is in the China (Hangzhou) region, set this parameter to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name";  // Required. The name of the bucket.
var key = "your object key";  // Required. The name of the object to be processed.

// Load the default configurations of OSS SDK. The configurations automatically read credential information (such as AccessKey) from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set the use of environment variables to obtain credentials for identity verification (Format: OSS_ACCESS_KEY_ID, 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); 

// Specify the storage location for the processed image. The bucket must be in the same region as the source bucket.
var targetBucket = bucket!;
// The name of the processed image (add the "process-" prefix).
var targetKey = $"process-{key}";
// Set the image processing rule: scale the image to a fixed size of 100 × 100 pixels.
var style = "image/resize,m_fixed,w_100,h_100";

// Base64-encode the destination storage information.
var targetNameBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(targetBucket));
var targetKeyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(targetKey));

// Construct the complete processing instruction: scale the image and then save it to the specified location.
var process = $"{style}|sys/saveas,o_{targetKeyBase64},b_{targetNameBase64}";

// Call the ProcessObjectAsync method to perform image processing and save the result.
var result = await client.ProcessObjectAsync(new OSS.Models.ProcessObjectRequest()
{
    Bucket = bucket,
    Key = key,
    Process = process
});

// Print the result information.
Console.WriteLine("ProcessObject done");  // The operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}");  // The HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}");  // The request ID, which is used by Alibaba Cloud to troubleshoot issues.
Console.WriteLine("Response Headers:");  // The response headers.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));   // Traverse and print all response headers.

References