OSS provides two methods for retrieving object metadata in a versioning-enabled bucket. HeadObjectAsync returns all metadata, while GetObjectMetaAsync returns a lightweight subset — useful when you only need to check size, integrity, or modification time without downloading the object.
| Method | Returns | Use when |
|---|---|---|
HeadObjectAsync | All metadata (full response headers) | You need content type, custom metadata, or other extended properties |
GetObjectMetaAsync | Content-Length, ETag, Last-Modified, x-oss-version-id, x-oss-hash-crc64ecma | You only need to verify size, checksum, or last-modified time |
Prerequisites
Before you begin, make sure you have:
A versioning-enabled OSS bucket
The
oss:GetObjectpermission on the target object. For details, see Grant custom permissions to a RAM userThe
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with valid AccessKey credentials
Note: The examples below use the China (Hangzhou) region (cn-hangzhou) with a public endpoint. If your application runs on an Alibaba Cloud service in the same region as your bucket, use an internal endpoint to reduce latency and avoid data transfer charges. For supported regions and endpoints, see OSS regions and endpoints.Retrieve all object metadata
Use HeadObjectAsync to get the complete set of metadata for a specific object version. The response includes all standard HTTP headers and any custom metadata stored with the object.
using OSS = AlibabaCloud.OSS.V2;
var region = "cn-hangzhou"; // Region where the bucket is located
var endpoint = null as string; // Optional: override the default endpoint
var bucket = "<your-bucket-name>"; // Replace with your bucket name
var key = "<your-object-key>"; // Replace with your object key
// Initialize the client with credentials from environment variables
var cfg = OSS.Configuration.LoadDefault();
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;
if (endpoint != null)
{
cfg.Endpoint = endpoint;
}
using var client = new OSS.Client(cfg);
try
{
var result = await client.HeadObjectAsync(new OSS.Models.HeadObjectRequest()
{
Bucket = bucket,
Key = key,
});
Console.WriteLine($"Status code: {result.StatusCode}");
Console.WriteLine($"Request ID: {result.RequestId}");
// Access specific metadata fields from the response headers
Console.WriteLine($"Content-Type: {result.Headers["Content-Type"]}");
Console.WriteLine($"Content-Length: {result.Headers["Content-Length"]}");
Console.WriteLine($"ETag: {result.Headers["ETag"]}");
Console.WriteLine($"Last-Modified: {result.Headers["Last-Modified"]}");
Console.WriteLine($"Version ID: {result.Headers["x-oss-version-id"]}");
}
catch (Exception ex)
{
Console.WriteLine($"Request failed: {ex.Message}");
}Replace the following placeholders:
| Placeholder | Description | Example |
|---|---|---|
<your-bucket-name> | Name of your OSS bucket | my-bucket |
<your-object-key> | Key (path) of the object | images/photo.jpg |
For the complete sample, see HeadObject.cs.
Retrieve partial object metadata
Use GetObjectMetaAsync when you only need a lightweight check — for example, to verify file size before downloading, or to confirm integrity using the CRC checksum. This operation returns only the following five fields and does not return the object body.
| Field | Description |
|---|---|
Content-Length | Object size in bytes |
ETag | Entity tag representing the object version |
Last-Modified | Date and time the object was last modified |
x-oss-version-id | Version ID of the object |
x-oss-hash-crc64ecma | 64-bit CRC checksum (CRC-64/ECMA) for data integrity verification |
using OSS = AlibabaCloud.OSS.V2;
var region = "cn-hangzhou"; // Region where the bucket is located
var endpoint = null as string; // Optional: override the default endpoint
var bucket = "<your-bucket-name>"; // Replace with your bucket name
var key = "<your-object-key>"; // Replace with your object key
// Initialize the client with credentials from environment variables
var cfg = OSS.Configuration.LoadDefault();
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;
if (endpoint != null)
{
cfg.Endpoint = endpoint;
}
using var client = new OSS.Client(cfg);
try
{
var result = await client.GetObjectMetaAsync(new OSS.Models.GetObjectMetaRequest()
{
Bucket = bucket,
Key = key,
});
Console.WriteLine($"Status code: {result.StatusCode}");
Console.WriteLine($"Request ID: {result.RequestId}");
Console.WriteLine($"Content-Length: {result.Headers["Content-Length"]}");
Console.WriteLine($"ETag: {result.Headers["ETag"]}");
Console.WriteLine($"Last-Modified: {result.Headers["Last-Modified"]}");
Console.WriteLine($"Version ID: {result.Headers["x-oss-version-id"]}");
Console.WriteLine($"CRC-64: {result.Headers["x-oss-hash-crc64ecma"]}");
}
catch (Exception ex)
{
Console.WriteLine($"Request failed: {ex.Message}");
}Replace the placeholders <your-bucket-name> and <your-object-key> as described in the table above.
For the complete sample, see GetObjectMeta.cs.