Download a file (Go SDK V1)
By default, the GetObject operation returns only the current version of an object from a versioning-enabled bucket.
Background information
When you call the GetObject operation on a bucket, one of three scenarios occurs:
-
If the current version of the object is a delete marker, OSS returns 404 Not Found.
-
If you specify an object's versionId in the query parameter, the specified version is returned. If you set the versionId to "null", the object version that has a null versionId is returned.
-
If you try to retrieve a delete marker by specifying its versionId, OSS returns 405 Method Not Allowed.
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 download an object, you must have the
oss:GetObjectpermission. For more information, see Grant a custom policy.
Sample code
The following code provides an example of how to download a file.
package main
import (
"log"
"net/http"
"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)
}
// Set yourBucketName to the name of the bucket.
bucketName := "yourBucketName"
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
}
var retHeader http.Header
// Download the file of the yourObjectVersionId version to the cache.
// Set yourObjectName to the full path of the object. The full path does not include the bucket name.
objectName := "yourObjectName"
objectVersionId := "yourObjectVersionId"
_, err = bucket.GetObject(objectName, oss.VersionId(objectVersionId), oss.GetResponseHeader(&retHeader))
if err != nil {
log.Fatalf("Failed to get object '%s' with version ID '%s': %v", objectName, objectVersionId, err)
}
// Print x-oss-version-id.
versionId := oss.GetVersionId(retHeader)
log.Printf("x-oss-version-id: %s", versionId)
}
References
-
For the complete sample code for downloading a file, see the GitHub example.
-
For more information about the API operation used to download a file, see GetObject.