In simple uploads, you can upload objects of the following types: strings, bytes, Unicode, network streams, and local files.

You can use the bucket.put_object method to upload an object. The following table describes the types of inputs supported by this method.

Type Upload method
Strings Direct upload.
Bytes Direct upload.
Unicode Convert to bytes encoded in UTF-8 to upload.
Local files Upload as file objects. The file objects must be opened in the binary mode (such as the rb mode).
Network streams Upload as iterable objects through chunked encoding.
Note If you upload an object with the same name as that of an accessible existing object, the existing object is overwritten by the uploaded object and a 200 OK status code is returned.

For more information about PutObject, see Detail analysis.

Upload strings

The following code provides an example on how 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 https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# Upload the object.
# If you need to set the object storage class and access permissions when uploading the object, set the headers in the put_object function as follows.
# headers = dict()
# headers["x-oss-storage-class"] = "Standard"
# headers["x-oss-object-acl"] = oss2.OBJECT_ACL_PRIVATE
# result = bucket.put_object('<yourObjectName>', 'content of object', headers=headers)
result = bucket.put_object('<yourObjectName>', 'content of object')

# Obtain the HTTP status code.
print('http status: {0}'.format(result.status))
# Obtain the unique request ID. We recommend that you add this parameter in the program logs.
print('request_id: {0}'.format(result.request_id))
# Obtain the ETag returned by the put_object method.
print('ETag: {0}'.format(result.etag))
# Obtain the HTTP response header.
print('date: {0}'.format(result.headers['date']))
			

Upload bytes

The following code provides an example on how 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 https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

bucket.put_object('<yourObjectName>', b'content of object')

			

Upload Unicode

The following code provides an example on how to upload Unicode:

# -*- 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 https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

bucket.put_object('<yourObjectName>', u'content of object')
			

Upload network streams

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 https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# The requests.get method returns an iterable object. In this case, the Python SDK uploads the object through chunked encoding.
input = requests.get('http://www.aliyun.com')
bucket.put_object('<yourObjectName>', input)
			

Upload local files

The following code provides an example on how to upload a local file to OSS:

# -*- 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 https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# The object must be opened in binary mode, because the number of bytes in the object must be available before the upload.
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 file 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)
			

The OSS Python SDK also provides a more convenient method to upload local files.

bucket.put_object_from_file('<yourObjectName>', '<yourLocalFile>')