All Products
Search
Document Center

Object Storage Service:Download objects as files

Last Updated:Sep 11, 2023

This topic describes how to download an object from a bucket to your computer.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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 Initialization.

  • To download an object as a local file, you must have the oss:GetObject permission. For more information, see Common examples of RAM policies.

Sample code

The following code provides an example on how to download exampleobject.txt from the exampledir directory of examplebucket to the D:\localpath path. After the object is downloaded, the local file is named examplefile.txt.

package main

import (
	"fmt"
	"os"

	"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 {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// 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. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucket, err := client.Bucket("examplebucket")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Download the object to the specified local path. If a file that has the same name already exists, the downloaded object overwrites the file. Otherwise, the downloaded object is saved in the path. 
	// If you do not specify a path for the downloaded object, the downloaded object is saved to the path of the project to which the sample program belongs. 
	// Specify the full path of the object that you want to download from the bucket. Example: exampledir/exampleobject.txt. Then, specify the full local path to which you want to download the object. Example: D:\\localpath\\examplefile.txt. Do not include the bucket name in the full path. 
	err = bucket.GetObjectToFile("exampledir/exampleobject.txt", "D:\\localpath\\examplefile.txt")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
}

References

  • For the complete sample code that is used to download an object as a local file, visit GitHub.

  • For more information about the API operation that you can call to download an object as a local file, see GetObject.