Todos os produtos
Search
Central de documentação

Object Storage Service:Gerenciar links simbólicos (C# SDK V2)

Última atualização: Sep 24, 2026

O recurso de link simbólico facilita o acesso a objetos usados com frequência em um bucket. Após criar um link simbólico, você pode utilizá-lo para acessar um objeto de maneira semelhante a um atalho no Windows. Este tópico descreve como usar o OSS C# SDK para criar e obter links simbólicos.

Observações

  • O código de exemplo neste tópico usa a região China (Hangzhou) como referência. O ID da região é cn-hangzhou. Por padrão, um endpoint público é usado. Ao acessar o OSS a partir de outros produtos da Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre os mapeamentos entre regiões e endpoints do OSS, consulte OSS regions and endpoints.

  • Para criar um link simbólico, é necessário ter a permissão oss:PutObject. Para obter um link simbólico, é necessário ter a permissão oss:GetObject. Para mais informações, consulte Grant custom access policies to RAM users.

Código de exemplo

Criar um link simbólico

Use o código a seguir para criar um link simbólico.

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. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
var endpoint = null as string;  // Optional. Specify the domain name used to access OSS. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name";  // Required. The bucket name.
var key = "your object name";  // Required. The object name.
var target = "your symlink name";  // Required. The symbolic link name.

// Load the default configurations of the 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); 

// Call the PutSymlinkAsync method to create a symbolic link that points to an object in the bucket.
var result = await client.PutSymlinkAsync(new()
{
    Bucket = bucket,
    Key = key,
    SymlinkTarget = target,
});

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

Obter um link simbólico

Use o código a seguir para obter um link simbólico.

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. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
var endpoint = null as string;  // Optional. Specify the domain name used to access OSS. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name";  // Required. The bucket name.
var key = "your object name";  // Required. The object name.

// Load the default configurations of the 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); 

// Call the GetSymlinkAsync method to obtain the symbolic link information of the target object.
var result = await client.GetSymlinkAsync(new()
{
    Bucket = bucket,
    Key = key
});

// Print the result.
Console.WriteLine("GetSymlink done");  // Indicate that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}");  // HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}");  // The request ID, which is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:");  // Response headers.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));  // Traverse and print all response headers.
Console.WriteLine($"SymlinkTarget: {result.SymlinkTarget}");  // Output the symbolic link information of the target object.

Referências

  • Para o código de exemplo completo sobre como criar um link simbólico, consulte PutSymlink.cs.

  • Para o código de exemplo completo sobre como obter um link simbólico, consulte GetSymlink.cs.