By default, the GetObjectMeta operation retrieves the metadata of the current version of an object in a versioning-enabled bucket. To retrieve the metadata of a specific version, specify the version ID in the request.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.
To query object metadata, you must have the
oss:GetObjectpermission. For more information, see Attach a custom policy to a RAM user.
Sample code
The following code shows how to retrieve the metadata of a specific version of an object:
package main
import (
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the full path of the object. Do not include the bucket name. Example: exampleobject.txt.
objectName := "exampleobject.txt"
// Specify the name of the bucket where the object is located. Example: examplebucket.
bucketName := "examplebucket"
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
}
// Get partial metadata of a specific version of the object.
// Set youObjectVersionId to the version ID of the object.
versionId := "youObjectVersionId"
props, err := bucket.GetObjectMeta(objectName, oss.VersionId(versionId))
if err != nil {
log.Fatalf("Failed to get object meta: %v", err)
}
log.Printf("Object Meta (Partial): %+v", props)
// Get all metadata of a specific version of the file.
props, err = bucket.GetObjectDetailedMeta(objectName, oss.VersionId(versionId))
if err != nil {
log.Fatalf("Failed to get object detailed meta: %v", err)
}
log.Printf("Object Meta (Detailed): %+v", props)
}
References
For the complete sample code for retrieving file metadata, see GitHub example.
For more information about the API operations that are used to retrieve file metadata, see GetObjectMeta and GetObjectDetailedMeta.