All Products
Search
Document Center

Object Storage Service:Simple upload

Last Updated:Oct 09, 2023

You can use the put_object method to upload a single object. This method is called simple upload. You can use simple upload to upload the following types of data: strings, bytes, Unicode characters, network streams, and local files.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

  • To perform simple upload, you must have the oss:PutObject permission. For more information, see Attach a custom policy to a RAM user.

Usage notes

If you upload an object that has the same name as an existing object, the existing object is overwritten by the uploaded object.

The following table lists the common parameters that you must configure when you upload an object.

Parameter

Description

bucket_name

The name of the bucket.

The name of a bucket must comply with the following naming conventions:

  • The name can contain only lowercase letters, digits, and hyphens (-).

  • The name must start and end with a lowercase letter or a digit.

  • The name must be 3 to 63 characters in length.

object_name

The full path of the object that you want to upload. Do not include the bucket name in the full path.

Object names must comply with the following conventions:

  • The object name must be encoded in UTF-8.

  • The name must be 1 to 1,023 characters in length.

  • The name cannot start with a forward slash (/) or a backslash (\).

Upload a string

The following code provides an example on how to upload a string to an object named exampleobject.txt in a bucket named examplebucket:

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

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# Upload the object. 
# You can configure the x-oss-storage-class and x-oss-object-acl headers for put_object to specify the storage class and access control list (ACL) of the object that you want to upload. 
# headers = dict()
# headers["x-oss-storage-class"] = "Standard"
# headers["x-oss-object-acl"] = oss2.OBJECT_ACL_PRIVATE
# Specify the full path of the object and the string that you want to upload. Do not include the bucket name in the full path. 
# result = bucket.put_object('exampleobject.txt', 'Hello OSS', headers=headers)
result = bucket.put_object('exampleobject.txt', 'Hello OSS')

# Display the returned HTTP status code. 
print('http status: {0}'.format(result.status))
# Display 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))
# Display 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))
# Display the HTTP response headers. 
print('date: {0}'.format(result.headers['date']))

Upload bytes

The following code provides an example on how to upload bytes to an object named exampleobject.txt in a bucket named examplebucket:

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# Specify the full path of the object and the bytes that you want to upload. Do not include the bucket name in the full path. 
bucket.put_object('exampleobject.txt', b'Hello OSS')

Upload Unicode characters

The following code provides an example on how to upload Unicode characters to an object named exampleobject.txt in a bucket named examplebucket. When you upload Unicode characters, OSS converts the characters into UTF-8-encoded bytes.

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

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# Specify the full path of the object and the Unicode characters that you want to upload. Do not include the bucket name in the full path. 
bucket.put_object('exampleobject.txt', u'Hello OSS')

Upload a network stream

The following code provides an example on how to upload a network stream to an object named exampleobject.txt in a bucket named examplebucket. OSS uploads network streams as iterable objects by means of chunked encoding.

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

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# The requests.get method returns an iterable object. OSS SDK for Python uploads the object by using chunked encoding. 
# Specify the address of the network stream. 
input = requests.get('http://www.aliyun.com')
# Specify the full path of the object. Do not include the bucket name in the full path. 
bucket.put_object('exampleobject.txt', input)

Upload a local file

The following code provides an example on how to upload a local file named examplefile.txt to a bucket named examplebucket. The uploaded file is stored as an object named exampleobject.txt in OSS. OSS uploads local files as file objects. During upload, OSS opens the files in binary mode, such as the rb mode.

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

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# The file must be opened in binary mode. 
# Specify the full path of the local file. By default, if you do not specify the full path of the local file, the local file is uploaded from the path of the project to which the sample program belongs. 
with open('D:\\localpath\\examplefile.txt', 'rb') as fileobj:
    # Use the seek method to read data from byte 1,000 of the file. The data is uploaded from byte 1000 to the last byte of the local file. 
    fileobj.seek(1000, os.SEEK_SET)
    # Use the tell method to obtain the current position. 
    current = fileobj.tell()
    # Specify the full path of the object. Do not include the bucket name in the full path. 
    bucket.put_object('exampleobject.txt', fileobj)

OSS SDK for Python also provides a more convenient method to upload local files. The following code provides an example on how to use this method to upload a local file:

# Specify the full path of the object and the full path of the local file. Do not include the bucket name in the full path of the object. 
# If the path of the local file is not specified, the file is uploaded to the path of the project to which the sample program belongs. 
bucket.put_object_from_file('exampleobject.txt', 'D:\\localpath\\examplefile.txt')

FAQ

How do I obtain the object URL after an object is uploaded?

If you upload an object by using OSS SDKs, the object URL is not contained in the response. For more information about how to obtain object URLs, see Obtain the URL of a single object or the URLs of multiple objects.

How do I check whether the size of an uploaded object is the same as that of a local file?

CRC is enabled by default when you upload or download objects to ensure data integrity. If the size of the uploaded object is different from that of the local file, an error InconsistentError is returned.

References

  • For the complete sample code that is used to perform simple upload, visit GitHub.

  • For more information about the API operation that you can call to perform simple upload, see PutObject.