This topic describes how to download an object from OSS to a local file.

You can run the following code to download the specified OSS object to a local file:

# -*- coding: utf-8 -*-
import oss2

# It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
auth = oss2. Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
#  This example uses the endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
bucket = oss2. Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# Download an object to your local file. If the name of the object is the same as that of your local file, the object will replace the local file. If the name of the object is different from that of your local file, the object will be downloaded and a new file will be added to your local device.
bucket.get_object_to_file('<yourObjectName>', '<yourLocalFile>')