Obtain file metadata (C# SDK V2)

Updated at:

This topic describes how to obtain the metadata of an object in a versioning-enabled bucket.

Prerequisites

  • The sample code in this topic uses the China (Hangzhou) region (cn-hangzhou) as an example. By default, a public endpoint is used. If you access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see OSS regions and endpoints.

  • To obtain object metadata, you must have the oss:GetObject permission. For more information, see Grant custom permissions to a RAM user.

Sample code

Use the HeadObjectAsync method to obtain all the metadata of an object

The following sample code shows how to use the HeadObjectAsync method to obtain all 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 HeadObjectAsync method to obtain all the metadata of the destination object.
var result = await client.HeadObjectAsync(new OSS.Models.HeadObjectRequest()
{
    Bucket = bucket,
    Key = key,
});

// Print the result.
Console.WriteLine("HeadObject 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.

Use the GetObjectMetaAsync method to obtain part of the metadata of an object

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.

References

  • For sample code on obtaining all the metadata of an object, see HeadObject.cs.

  • For sample code on obtaining part of the metadata of an object, see GetObjectMeta.cs.