You can use simple upload to upload the following types of data: strings, bytes, Unicode characters, network streams, and local files.
Usage notes
If you upload an object whose name is the same as that of an accessible existing object, the existing object is overwritten by the uploaded object and 200 OK is returned.
The following table lists the common parameters that you must configure when you upload an object.
Parameter | Description |
---|---|
yourBucketName | The bucket name.
The bucket name must comply with the following conventions:
|
yourObjectName | The object name that includes the complete object path excluding the bucket name.
The object name must comply with the following conventions:
|
For more information about how to perform simple upload, see PutObject.
Upload strings
The following code provides an example on how to use the bucket.put_object
method to upload a string:
# -*- coding: utf-8 -*-
import oss2
# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
# Upload an object.
# You can configure headers for put_object to configure the storage class and ACL of the object that you want to upload. The following code provides an example on how to configure headers for put_object.
# headers = dict()
# headers["x-oss-storage-class"] = "Standard"
# headers["x-oss-object-acl"] = oss2.OBJECT_ACL_PRIVATE
# yourObjectName is the object name, including the complete object path without the bucket name. Example: example/folder/abc.jpg.
# result = bucket.put_object('<yourObjectName>', 'content of object', headers=headers)
result = bucket.put_object('<yourObjectName>', 'content of object')
# Obtain the returned HTTP status code.
print('http status: {0}'.format(result.status))
# Obtain the request ID. A request ID uniquely identifies a request. We recommend that you add this parameter to the logs.
print('request_id: {0}'.format(result.request_id))
# Obtain the ETag value returned by the put_object method. The ETag value of an object is used to identify the object content.
print('ETag: {0}'.format(result.etag))
# Obtain the HTTP response headers.
print('date: {0}'.format(result.headers['date']))
Upload bytes
The following code provides an example on how to use the bucket.put_object
method to upload bytes:
# -*- coding: utf-8 -*-
import oss2
# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
bucket.put_object('<yourObjectName>', b'content of object')
Upload Unicode characters
When you use the bucket.put_object
method to upload Unicode characters, OSS converts the characters into UTF-8-encoded
bytes. The following code provides an example on how to upload Unicode characters:
# -*- coding: utf-8 -*-
import oss2
# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
bucket.put_object('<yourObjectName>', u'content of object')
Upload network streams
OSS uploads network streams as iterable objects by using chunked encoding. The following code provides an example on how to upload a network stream:
# -*- coding: utf-8 -*-
import oss2
import requests
# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
# The requests.get method returns an iterable object. In this case, OSS SDK for Python uploads the object by using chunked encoding.
input = requests.get('http://www.aliyun.com')
bucket.put_object('<yourObjectName>', input)
Upload local files
OSS uploads local files as file objects and opens the files in binary mode during upload. The following code provides an example on how to upload a local file:
# -*- coding: utf-8 -*-
import oss2
# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
# The object must be opened in binary mode.
with open('<yourLocalFile>', 'rb') as fileobj:
# Use the seek method to read data from the specified position of the 1000th byte. The data is uploaded from the specified 1000th byte until the entire object is uploaded.
fileobj.seek(1000, os.SEEK_SET)
# Use the tell method to obtain the current position.
current = fileobj.tell()
bucket.put_object('<yourObjectName>', fileobj)
OSS SDK for Python also provides a more convenient method to upload local files. The following code provides an example of this method:
# yourObjectName is the object name, including the complete object path without the bucket name. Example: example/folder/abc.jpg.
# yourLocalFile is the complete path of the local file that you want to upload. Example: /users/local/abc.jpg.
bucket.put_object_from_file('<yourObjectName>', '<yourLocalFile>')