Upload File to Bucket via PUT 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
PUT request to upload file to Bucket 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 upload(access_key, secret_key)
7def upload(ak, sk, token=None):
8 """Upload file"""
9 bucket_name = "bucketName"
10 object_key = "/aaabbb.png" # object_key is the name to be saved after upload, 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 = "PUT" # 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
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# If there is a token parameter, add it to the header
26 if token is not None:
27 header['x-bce-security-token'] = token
28# Read the file as a binary stream
29 local_path = "/local path/filename"
30 with open(local_path, "rb") as f:
31# Initiate a put upload request
32 resp = requests.put(url=request_url, headers=header, data=f)
33 print(resp)
34if __name__ == '__main__':
35# Upload 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 upload(access_key, secret_key, token)
9def upload(ak, sk, token=None):
10 """Upload file"""
11 bucket_name = "bucketName"
12 object_key = "/aaabbb.png" # object_key is the name to be saved after upload, 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 = "PUT" # 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
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# Read the file as a binary stream
31 local_path = "/local path/filename"
32 with open(local_path, "rb") as f:
33# Initiate a put upload request
34 resp = requests.put(url=request_url, headers=header, data=f)
35 print(resp)
36if __name__ == '__main__':
37# Upload files using the temporary ak-sk method
38 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.
Note:
- Save the entire code from the example above into a single .py file and execute it.
- A response of 200 indicates that the file has been uploaded successfully, and you can also check whether the relevant file exists in the console

