Todos os produtos
Search
Central de documentação

Object Storage Service:Processamento síncrono (OSS SDK for C# V2)

Última atualização: Jul 03, 2026

O processamento síncrono (x-oss-process) exige que o programa aguarde a conclusão de uma tarefa antes de continuar. Use o OSS SDK for C# V2 para executar o processamento síncrono de imagens.

Precauções

O código de exemplo usa a região China (Hangzhou) (cn-hangzhou) e o endpoint público. Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para obter mais informações sobre regiões e endpoints compatíveis, consulte Regiões e endpoints.

Código de exemplo

O exemplo a seguir mostra como redimensionar uma imagem e salvar o resultado em um bucket específico.

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.

Referências