Exclua buckets desnecessários com o C# SDK V2.
Pré-requisitos
O código de exemplo usa a região China (Hangzhou) (
cn-hangzhou) e um 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 mais informações, consulte Regiões e endpoints.Todos os pontos de acesso do bucket devem estar excluídos. Para mais informações, consulte Pontos de acesso.
-
Todos os objetos no bucket devem estar excluídos.
ImportanteSe o versionamento estiver ativado para o bucket, certifique-se de excluir todas as versões atuais e anteriores dos objetos. Para mais informações, consulte Versionamento.
Para poucos objetos, exclua-os manualmente. Para mais informações, consulte Excluir objetos.
Para muitos objetos, configure uma regra de ciclo de vida para exclusão automática. Para mais informações, consulte Ciclo de vida.
Todas as partes geradas por uploads multipart ou retomáveis no bucket devem estar excluídas. Para mais informações, consulte Excluir partes.
Permissões
Por padrão, uma conta Alibaba Cloud tem permissões totais. Usuários RAM ou funções RAM vinculados a essa conta não têm permissões por padrão. A conta Alibaba Cloud ou o administrador deve conceder as permissões de operação por meio de políticas RAM ou Bucket Policy.
|
API |
Ação |
Descrição |
|
DeleteBucket |
|
Exclui um bucket. |
Código de exemplo
O código a seguir exclui um bucket:
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. In this example, China (Hangzhou) is used. 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 domain name used to access OSS. In this example, China (Hangzhou) is used. Set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
// Load the default configurations of the OSS SDK. The configurations automatically read credential information, such as the AccessKey pair, from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly configure the use of environment variables to obtain credentials for identity verification. The format is OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Specify the region of the bucket in the configurations.
cfg.Region = region;
// If an endpoint is specified, overwrite the default endpoint.
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance based on the configuration information.
using var client = new OSS.Client(cfg);
// Call the DeleteBucketAsync method to delete the specified bucket.
var result = await client.DeleteBucketAsync(new OSS.Models.DeleteBucketRequest()
{
Bucket = bucket
});
// Print the result information.
Console.WriteLine("DeleteBucket done"); // A message indicating that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}"); // The HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}"); // The request ID, which is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:"); // The response header.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headers.
Referências
Para obter o código de exemplo completo, consulte deleteBucket.cs.