Download Bucket File to Local via GET Request
Updated at:2025-11-03
Dependencies
To access a bucket using the original API, you must manually calculate the authentication information. The usage process is as follows:
- Prepare ak/sk
- Python 3.6 or higher environment
- Install the requests package:
pip3 install requests
GET request to download a bucket file demo
- Permanent ak-sk Method
Python
1# Copy the authentication string algorithm code from Basics (Must-Read): Generation of Authentication String
2def default_ak_sk():
3 """Using the fixed ak-sk method, i.e., permanent ak-sk"""
4 access_key = "ak" # Fixed ak
5 secret_key = "sk" # Fixed sk
6 download(access_key, secret_key)
7def download(ak, sk, token=None):
8 """Download a file from the bucket to the local machine"""
9 bucket_name = "bucketName" # Name of the accessed bucket
10 object_key = "/aaa.png" # When there are no parameters in the request url, the name of the accessed key is the path, note that it starts with '/'
11 region = "bj.bcebos.com" # The region where the bucket is located; the service domain name varies by region
12 http_method = "GET" # Type of request method: GET, PUT, HEAD, POST, DELETE
13 host = f"{bucket_name}.{region}" # Assemble the access domain name based on the bucket and service domain name; there are multiple ways, only one is shown here
14# Construct the accessed header information; additional fields can be added as needed, such as the content-type field
15 header = {
16 "host": host,
17 "x-bce-date": get_x_bce_date()
18 }
19# If the request has parameters in the url, they must also participate in authentication
20 param = {}
21# Generate the authentication string
22 auth_key = generate_bos_auth(object_key, ak, sk, http_method, header, param)
23# Construct the complete access url
24 request_url = f"http://{host}{object_key}?{AUTHORIZATION}={auth_key}"
25 print(request_url)
26# If there is a token parameter, add it to the header
27 if token is not None:
28 header['x-bce-security-token'] = token
29# GET request
30 resp = requests.get(url=request_url, headers=header)
31# Local file path
32 local_path = "/path/filename"
33 save_local(resp, local_path)
34if __name__ == '__main__':
35# Download files using the fixed ak-sk method
36 default_ak_sk()
- Temporary sts ak-sk-token method
Getsts ak-sk-token
Python
1# Copy the authentication string algorithm code from Basics (Must-Read): Generation of Authentication String
2def sts_ak_sk():
3 """Using the temporary ak-sk-token method"""
4 access_key = "8702e0c7f08211ec8168058160a92b86" # Temporarily applied sts ak
5 secret_key = 315fddebae1f4d65b561a296c5fbc2f3" # Temporarily applied sts sk
6# Example of temporary sts token information; please replace it with the self-applied ak/sk/token
7 token = "ZjkyZmQ2YmQxZTQ3NDcyNjk0ZTg1ZjYyYjlkZjNjODB8AAAAAJMCAACg8ipvvc5Ox8EFDupU5wGhWMBSjmRlbOexzgTxXUZHf+ZFUEWBFiDGGsLxoRG8FnEdmON/7tRwpmHk0mf9cnCwE7pzD1ovRASiLt7FfguU57A42Pk99QjTSxjkdzKF/Oo9wad3r757UC6p2evxb7vem8kHpQBx4yCt7hwoXhWrKNOAlCiD1s0JEa3/e6hRtiiV0Jntn1C4eT9nJm8EwFa4I2Q4nkSrIs21+IkbMdkpyedCwUZcYBAohHaa69U9CQvgc1i9CnFRE6MdMyXFectg7Fm7SUOpM0lVIu/TSGY7quCKFNkWZHdXm4Y1xBNj0vPyoaD5M6PhpfCegp394KnfxrLzJTq04pjuFLECZlbwzTR26jWLy/mWwrXFFIPO68NaqVlYsc8yNnu3xctAMD8bqrnyZ5P4Ph0/R0SDvZl6drQ6rcVB55D6CT1ZuNsU3aD6dFWucDTTJJQhLBfL7tBh7kg11zdPV4Z3b3QPAb4fs8O269M1ihh6ABoincn8WJSraExuC7DUi0J5pIS84yNR"
8 download(access_key, secret_key, token)
9def download(ak, sk, token=None):
10 """Download a file from the bucket to the local machine"""
11 bucket_name = "bucketName" # Name of the accessed bucket
12 object_key = "/aaa.png" # When there are no parameters in the request url, the name of the accessed key is the path, note that it starts with '/'
13 region = "bj.bcebos.com" # The region where the bucket is located; the service domain name varies by region
14 http_method = "GET" # Type of request method: GET, PUT, HEAD, POST, DELETE
15 host = f"{bucket_name}.{region}" # Assemble the access domain name based on the bucket and service domain name; there are multiple ways, only one is shown here
16# Construct the accessed header information; additional fields can be added as needed, such as the content-type field
17 header = {
18 "host": host,
19 "x-bce-date": get_x_bce_date()
20 }
21# If the request has parameters in the url, they must also participate in authentication
22 param = {}
23# Generate the authentication string
24 auth_key = generate_bos_auth(object_key, ak, sk, http_method, header, param)
25# Construct the complete access url
26 request_url = f"http://{host}{object_key}?{AUTHORIZATION}={auth_key}"
27# If there is a token parameter, add it to the header
28 if token is not None:
29 header['x-bce-security-token'] = token
30# GET request
31 resp = requests.get(url=request_url, headers=header)
32# Local file path
33 local_path = "/path/filename"
34 save_local(resp, local_path)
35if __name__ == '__main__':
36# Download files using the temporary ak-sk method
37 sts_ak_sk()
There is no difference in generating the authentication auth_key between temporary ak-sk-token and permanent ak-sk, except that the x-bce-security-token parameter (equal to the applied token)needs to be added to the header information. Save the complete code in the above example into a single py file to execute.
