Note
You can use the GetObjectMetaAsync method to obtain only a part of the object metadata, such as the content length (Content-Length), ETag, last modified time (Last-Modified), version ID (x-oss-version-id), and 64-bit CRC value (x-oss-hash-crc64ecma) of the object.
The following sample code shows how to use the GetObjectMetaAsync method to obtain part of the metadata for a specific version of an object.
using OSS = AlibabaCloud.OSS.V2; // Create an alias for the 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 located in the China (Hangzhou) region, set the region to cn-hangzhou.
var endpoint = null as string; // Optional. The endpoint used to access OSS. For example, if the bucket is located 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 bucket.
var key = "your object key"; // Required. The name of the object.
// 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 configure the client to retrieve credentials from environment variables for identity verification. The format is OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket.
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 GetObjectMetaAsync method to obtain part of the metadata of the destination object.
var result = await client.GetObjectMetaAsync(new OSS.Models.GetObjectMetaRequest()
{
Bucket = bucket,
Key = key,
});
// Print the result.
Console.WriteLine("GetObjectMeta 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 for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:"); // The response headers.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headers.