All Products
Search
Document Center

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

Last Updated:Aug 06, 2025

In synchronous processing (`x-oss-process`), a program must wait for a task to complete before it can proceed to other tasks. This topic describes how to perform synchronous image processing using the OSS SDK for C# V2.

Precautions

The sample code in this topic uses the China (Hangzhou) region (region ID: 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 an internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints.

Sample code

The following code provides an example of how to use the OSS SDK for C# V2 to scale an image and save the processed image to a specified bucket for persistence.

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