Cette rubrique explique comment télécharger un objet depuis un compartiment vers votre ordinateur.
Notes d'utilisation
Dans cette rubrique, le point de terminaison public de la région Chine (Hangzhou) est utilisé. Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région que OSS, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.
Dans cette rubrique, les identifiants d'accès sont obtenus à partir des variables d'environnement. Pour savoir comment configurer les identifiants d'accès, consultez Configurer les identifiants d'accès.
Cette rubrique illustre la création d'une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Configurer un client (SDK Go V1).
Pour télécharger un objet en tant que fichier local, vous devez disposer de l'autorisation
oss:GetObject. Pour plus d'informations, consultez Accorder une politique personnalisée.
Exemples
L'exemple de code suivant montre comment télécharger l'objet exampleobject.txt du répertoire exampledir d'un compartiment nommé examplebucket vers D:\localpath. Une fois l'objet téléchargé, le fichier local est nommé 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.
// 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.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
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 name of the bucket. Example: examplebucket.
bucketName := "examplebucket" // Replace examplebucket 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 the specified local path. If a file that has the same name already exists in the path, the downloaded object overwrites the file. If no file that has the same name exists in the path, the downloaded object is saved in the path.
// If you do not specify a local 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 path of the local file in which you want to store the downloaded object. Example: D:\\localpath\\examplefile.txt. Do not include the bucket name in the full path.
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.")
}
Scénarios courants
Références
Pour consulter l'exemple de code complet utilisé pour télécharger un objet en tant que fichier local, rendez-vous sur GitHub.
Pour plus d'informations sur l'opération API permettant de télécharger un objet en tant que fichier local, consultez GetObjectToFile.