This topic describes how to upload an object to a versioning-enabled bucket using simple upload, append upload, or multipart upload.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials using OSS SDK for Python 1.0.
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 upload files, you must have the
oss:PutObjectpermission. For more information, see Grant custom permissions to a RAM user.
Simple upload
In a bucket with versioning enabled, OSS automatically generates a unique version ID for each newly added object and returns the ID in the x-oss-version-id response header. In a bucket with versioning suspended, the version ID of a newly added object is "null". If you upload an object with the same name, the new object overwrites the previous one. OSS ensures that only one version of an object can have a "null" version ID.
The following code shows how to perform a simple upload:
# -*- 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.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the endpoint is located, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Upload the file.
result = bucket.put_object('yourObjectName', 'content of object')
# HTTP status code.
print('http response code: {0}'.format(result.status))
# View the version ID of the uploaded object.
print('put object version:', result.versionid)Append upload
In a versioning-enabled bucket, you can perform an append operation (AppendObject) only on the current version of an appendable object. You cannot perform an AppendObject operation on a previous version of an appendable object.
When you perform an AppendObject operation on the current version of an appendable object, OSS does not create a previous version for the object.
When you perform a PutObject or DeleteObject operation on the current version of an appendable object, OSS saves the current version as a previous version. You can no longer append data to this object.
You cannot perform an AppendObject operation on the current version of a non-appendable object, such as a normal object or a delete marker.
The following code shows how to perform an append upload:
# -*- 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.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the endpoint is located, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Set the append position (the Position parameter) to 0 for the first upload.
result = bucket.append_object('yourObjectName', 0, 'content of first append')
# View the version ID of the object to which data is appended.
print('append object versionid:', result.versionid)
# If this is not the first append operation, you can obtain the append position from the bucket.head_object method or the next_position property in the return value of the previous append operation.
bucket.append_object('yourObjectName', result.next_position, 'content of second append')Multipart upload
In a versioning-enabled bucket, when you call the CompleteMultipartUpload API operation to complete a multipart upload, OSS generates a unique version ID for the entire object and returns the ID in the x-oss-version-id response header.
The following code shows how to perform a multipart upload:
# -*- coding: utf-8 -*-
import os
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2 import SizedFileAdapter, determine_part_size
from oss2.models import PartInfo
# 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.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the endpoint is located, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
key = 'yourObjectName'
filename = 'yourLocalFile'
total_size = os.path.getsize(filename)
# The determine_part_size method is used to determine the part size.
part_size = determine_part_size(total_size, preferred_size=100 * 1024)
# Initialize the multipart upload.
upload_id = bucket.init_multipart_upload(key).upload_id
parts = []
# Upload parts one by one.
with open(filename, 'rb') as fileobj:
part_number = 1
offset = 0
while offset < total_size:
num_to_upload = min(part_size, total_size - offset)
# The SizedFileAdapter(fileobj, size) method generates a new file object and recalculates the start position for appending data.
result = bucket.upload_part(key, upload_id, part_number,
SizedFileAdapter(fileobj, num_to_upload))
parts.append(PartInfo(part_number, result.etag))
offset += num_to_upload
part_number += 1
# Complete the multipart upload.
result = bucket.complete_multipart_upload(key, upload_id, parts)
# View the versionid of the uploaded file in the response.
print('result.versionid:', result.versionid)
# Verify the multipart upload.
with open(filename, 'rb') as fileobj:
assert bucket.get_object(key).read() == fileobj.read()References
For more information about the API operation for simple upload, see PutObject.
For more information about the API operation for append upload, see AppendObject.
For more information about the API operation for multipart upload, see CompleteMultipartUpload.