Este tema describe cómo usar el OSS SDK para C# para listar todos los objetos en un bucket específico.
Precauciones
El código de ejemplo de este tema usa la región China (Hangzhou) (
cn-hangzhou) como referencia. De forma predeterminada, se usa el endpoint público. Si desea acceder a OSS desde otros servicios de Alibaba Cloud en la misma región, use el endpoint interno. Para obtener más información sobre las regiones y endpoints que admite OSS, consulte OSS regions and endpoints.Este tema proporciona un ejemplo para leer las credenciales de acceso desde variables de entorno. Para obtener más información sobre cómo configurar las credenciales de acceso, consulte Configure access credentials.
Para listar objetos, debe contar con el permiso
oss:ListObjects. Para obtener más información, consulte Grant custom permissions to a RAM user.
Código de ejemplo
Use el siguiente código para listar todos los objetos en el bucket especificado.
using OSS = AlibabaCloud.OSS.V2; // Create an alias for the 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 the OSS service. 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 name of the destination bucket.
// 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 ListObjectsV2Paginator method to obtain all objects in the destination bucket.
var paginator = client.ListObjectsV2Paginator(new OSS.Models.ListObjectsV2Request()
{
Bucket = bucket
});
// Print the information of all objects in the bucket.
Console.WriteLine("Objects:");
await foreach (var page in paginator.IterPageAsync()) // Asynchronously traverse each page of results.
{
// Traverse each object on the current page.
foreach (var content in page.Contents ?? [])
{
// Print the object information: key, size (in bytes), and last modified time.
Console.WriteLine($"Object:{content.Key}, {content.Size}, {content.LastModified}");
}
}
Referencias
Para consultar el código de ejemplo completo, véase ListObjects.cs.