Download a file (Go SDK V1)

Updated at:

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:

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:

PlaceholderDescriptionExample
<yourEndpoint>Endpoint of the bucket's regionhttps://oss-cn-hangzhou.aliyuncs.com
<yourRegion>Region ID where the bucket is locatedcn-hangzhou
<yourBucketName>Name of your bucketmy-bucket
<yourObjectName>Full path of the object, excluding the bucket namelogs/2026/app.log
<yourObjectVersionId>Version ID of the object to downloadCAEQGBiBgIDV...

How versioning affects GetObject

When you call GetObject on a versioning-enabled bucket:

RequestOSS behavior
No version ID specifiedReturns the current version of the object
Version ID specifiedReturns the specified version
Version ID set to nullReturns 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 markerReturns 405 Method Not Allowed

References