Query object metadata (OSS SDK for Go 1.0)
By default, when you call the GetObjectMeta operation on an Object Storage Service (OSS) object in a versioning-enabled bucket, the metadata of only the current version of the object is returned. If you specify a version ID in the request, OSS returns the metadata of the specified version of the object.
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.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Configure a client (Go SDK V1).
-
To query object metadata, you must have the
oss:GetObjectpermission. For more information, see Grant a custom policy.
Examples
The following sample code provides an example on how to query 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 configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
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 in the full path. Example: exampleobject.txt.
objectName := "exampleobject.txt"
// Specify the name of the bucket in which the object is stored. Example: examplebucket.
bucketName := "examplebucket"
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
}
// Query part of the metadata of the specified version of the object.
// Specify 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)
// Query all metadata of the specified version of the object.
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 that is used to query object metadata, visit GitHub.
-
For more information about the API operation that you can call to query object metadata, see GetObjectMeta and GetObjectDetailedMeta.