All Products
Search
Document Center

Object Storage Service:Download an object to a local file

Last Updated:Mar 01, 2026

This topic describes how to download an Object Storage Service (OSS) object from a bucket to your local computer.

Prerequisites

  • Access credentials are configured. The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. For more information, see Configure access credentials using OSS SDK for Python 1.0.

  • An OSSClient instance is initialized. This topic creates an OSSClient instance by using an OSS endpoint. To create an instance by using custom domain names or Security Token Service (STS), see Initialization.

Usage notes

  • This topic uses the public endpoint of the China (Hangzhou) region. If you access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about regions and endpoints, see Regions and endpoints.

  • If a local file with the same name already exists at the destination path, the downloaded object overwrites it.

Sample code

The following example downloads the object testfolder/exampleobject.txt from the bucket examplebucket and saves it to a local path.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint and region. Example: China (Hangzhou).
# The region parameter is required for signature algorithm V4.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"

# Create a Bucket instance. Replace examplebucket with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Download the object to a local file.
# Specify the full path of the object (without the bucket name) and the local file path.
# Windows example:
bucket.get_object_to_file('testfolder/exampleobject.txt', 'D:\\localpath\\examplefile.txt')
# Linux/macOS example:
# bucket.get_object_to_file('testfolder/exampleobject.txt', '/home/user/localpath/examplefile.txt')

print("Download complete.")

We recommend that you wrap the download call in a try-except block to handle errors such as a nonexistent object or insufficient permissions.

References