This topic describes how to download an object from a bucket to a local file.
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 download an object to a local file, you must have the
oss:GetObjectpermission. For more information, see Grant custom access policies to a RAM user.
Sample code
The following code shows how to download the exampleobject.txt file from the exampledir folder in the examplebucket bucket to the local path D:\localpath\examplefile.txt.
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.
// 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 buckets in other regions, set the endpoint to the actual endpoint.
// 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 buckets in other regions, set the region to the actual region.
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 bucket name, for example, examplebucket.
bucketName := "examplebucket" // Replace with the actual bucket name.
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket: %v", err)
}
// Download the object to a local file and save it to the specified local path. If the specified local file exists, it is overwritten. If the specified local file does not exist, it is created.
// If you do not specify a local path, the downloaded file is saved to the local path of the project where the sample program resides.
// Specify the full path of the object, for example, exampledir/exampleobject.txt, and the full path of the local file, for example, D:\\localpath\\examplefile.txt. The full path of the object cannot contain the bucket name.
objectName := "exampledir/exampleobject.txt"
localFilePath := "D:\\localpath\\examplefile.txt"
err = bucket.GetObjectToFile(objectName, localFilePath)
if err != nil {
log.Fatalf("Failed to download file: %v", err)
}
log.Println("File downloaded successfully.")
}
Scenarios
References
For the complete sample code that shows how to download an object to a local file, see GitHub.
For more information about the API operation used to download an object to a local file, see GetObjectToFile.