Download a file (Go SDK V1)
Use the GetObject operation to download an object from a versioning-enabled bucket. By default, OSS returns the current version. Pass a version ID to retrieve a specific version.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket with versioning enabled
The
oss:GetObjectpermission attached to your RAM user. See Attach a custom policy to a RAM userThe
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with your access credentials. See Configure access credentials
Usage notes
The examples use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. See Regions and endpoints.
To create an OSSClient instance using a custom domain or Security Token Service (STS), see Configure OSSClient instances.
Download an object by version ID
The following example downloads a specific version of an object and reads the version ID from the response header.
package main
import (
"log"
"net/http"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Load access credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Replace <yourEndpoint> with the endpoint for your bucket's region,
// for example: https://oss-cn-hangzhou.aliyuncs.com
// Replace <yourRegion> with the region ID, for example: cn-hangzhou
clientOptions := []oss.ClientOption{
oss.SetCredentialsProvider(&provider),
oss.Region("<yourRegion>"),
oss.AuthVersion(oss.AuthV4),
}
client, err := oss.New("<yourEndpoint>", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
bucket, err := client.Bucket("<yourBucketName>")
if err != nil {
log.Fatalf("Failed to get bucket: %v", err)
}
// Download a specific version of an object.
// Set the full path of the object, excluding the bucket name.
// If the version ID belongs to a delete marker, OSS returns 405 Method Not Allowed.
// If the current version is a delete marker and no version ID is specified, OSS returns 404 Not Found.
objectName := "<yourObjectName>"
objectVersionId := "<yourObjectVersionId>"
var retHeader http.Header
_, err = bucket.GetObject(
objectName,
oss.VersionId(objectVersionId),
oss.GetResponseHeader(&retHeader),
)
if err != nil {
log.Fatalf("Failed to get object: %v", err)
}
// Print the x-oss-version-id returned in the response.
versionId := oss.GetVersionId(retHeader)
log.Printf("x-oss-version-id: %s", versionId)
}Replace the following placeholders with your actual values:
| Placeholder | Description | Example |
|---|---|---|
<yourEndpoint> | Endpoint of the bucket's region | https://oss-cn-hangzhou.aliyuncs.com |
<yourRegion> | Region ID where the bucket is located | cn-hangzhou |
<yourBucketName> | Name of your bucket | my-bucket |
<yourObjectName> | Full path of the object, excluding the bucket name | logs/2026/app.log |
<yourObjectVersionId> | Version ID of the object to download | CAEQGBiBgIDV... |
How versioning affects GetObject
When you call GetObject on a versioning-enabled bucket:
| Request | OSS behavior |
|---|---|
| No version ID specified | Returns the current version of the object |
| Version ID specified | Returns the specified version |
Version ID set to null | Returns the version whose version ID is null |
| Current version is a delete marker (no version ID) | Returns 404 Not Found |
| Version ID is the ID of a delete marker | Returns 405 Method Not Allowed |