By default, if you upload an object that has the same name as an existing object, the existing object is overwritten by the uploaded object. This topic describes how to configure the x-oss-forbid-overwrite request header to prevent existing objects from being overwritten by objects with the same names when you copy objects or perform simple 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.
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.
Simple upload
The following sample code provides an example on how to prevent existing objects from being overwritten by objects that have the same names when you perform simple upload:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before running the sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that 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.
# Specify whether to overwrite an existing object with the same name during a PutObject operation.
# If you do not specify x-oss-forbid-overwrite, the existing object is overwritten by default.
# If you set x-oss-forbid-overwrite to false, the existing object is allowed to be overwritten.
# If you set x-oss-forbid-overwrite to true, the existing object is not allowed to be overwritten. If an object with the same name exists, the program reports an error.
headers = {'x-oss-forbid-overwrite': 'true'}
result = bucket.put_object('yourObjectName', 'content of object', headers=headers)
# HTTP status code.
print('http status: {0}'.format(result.status))
# Request ID. The request ID is the unique identifier of the request. We recommend that you add this parameter to your program logs.
print('request_id: {0}'.format(result.request_id))
# ETag is a property specific to the return value of the put_object method.
print('ETag: {0}'.format(result.etag))
# HTTP header.
print('date: {0}'.format(result.headers['date']))Copy a file
Copy a small file
The following code shows how to prevent an object from being overwritten when you copy a small file:
# -*- coding: utf-8 -*- import oss2 from oss2.credentials import EnvironmentVariableCredentialsProvider # Obtain access credentials from environment variables. Before running the sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider()) # Specify the endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. # Set yourBucketName to the name of the bucket. bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName') # Specify whether to overwrite an existing object with the same name during a copy_object operation. # If you do not specify x-oss-forbid-overwrite, the existing object is overwritten by default. # If you set x-oss-forbid-overwrite to false, the existing object is allowed to be overwritten. # If you set x-oss-forbid-overwrite to true, the existing object is not allowed to be overwritten. If an object with the same name exists, the program reports an error. headers = dict() headers = {'x-oss-forbid-overwrite':'true'} bucket.copy_object('yourSourceBucketName', 'yourSourceObjectName', 'yourDestinationObjectName', headers=headers)Copy a large file
The following sample code provides an example on how to prevent an existing object from being overwritten by a large object with the same name when you copy the large object by using multipart copy:
# -*- coding: utf-8 -*- import oss2 from oss2.credentials import EnvironmentVariableCredentialsProvider from oss2.models import PartInfo from oss2 import determine_part_size # Obtain access credentials from environment variables. Before running the sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider()) # Specify the endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. endpoint = "https://oss-cn-hangzhou.aliyuncs.com" # Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that 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) src_object = 'yourSourceObjectName' dst_object = 'yourDestinationObjectName' total_size = bucket.head_object(src_object).content_length part_size = determine_part_size(total_size, preferred_size=100 * 1024) # Initialize the multipart copy. # Specify whether to overwrite an existing object with the same name during a file copy operation. # If you do not specify x-oss-forbid-overwrite, the existing object is overwritten by default. # If you set x-oss-forbid-overwrite to false, the existing object is allowed to be overwritten. # If you set x-oss-forbid-overwrite to true, the existing object is not allowed to be overwritten. If an object with the same name exists, the program reports an error. headers = dict() headers = {'x-oss-forbid-overwrite': 'true'} upload_id = bucket.init_multipart_upload(dst_object, headers=headers).upload_id parts = [] # Copy parts one by one. part_number = 1 offset = 0 while offset < total_size: num_to_upload = min(part_size, total_size - offset) byte_range = (offset, offset + num_to_upload - 1) result = bucket.upload_part_copy(bucket.bucket_name, src_object, byte_range, dst_object, upload_id, part_number) parts.append(PartInfo(part_number, result.etag)) offset += num_to_upload part_number += 1 # Complete the multipart copy. # Specify whether to overwrite an existing object with the same name during a file copy operation. # If you do not specify x-oss-forbid-overwrite, the existing object is overwritten by default. # If you set x-oss-forbid-overwrite to false, the existing object is allowed to be overwritten. # If you set x-oss-forbid-overwrite to true, the existing object is not allowed to be overwritten. If an object with the same name exists, the program reports an error. headers = dict() headers = {'x-oss-forbid-overwrite':'true'} bucket.complete_multipart_upload(dst_object, upload_id, parts, headers=headers)
Multipart upload
The following sample code provides an example on how to prevent an existing object from being overwritten by an object with the same name when you use multipart upload to upload the object:
# -*- coding: utf-8 -*-
import os
from oss2 import SizedFileAdapter, determine_part_size
from oss2.models import PartInfo
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before running the sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that 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.
# Specify whether to overwrite an existing object with the same name during a multipart upload operation.
# If you do not specify x-oss-forbid-overwrite, the existing object is overwritten by default.
# If you set x-oss-forbid-overwrite to false, the existing object is allowed to be overwritten.
# If you set x-oss-forbid-overwrite to true, the existing object is not allowed to be overwritten. If an object with the same name exists, the program reports an error.
headers = {'x-oss-forbid-overwrite': 'true'}
upload_id = bucket.init_multipart_upload(key, headers=headers).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 starting append position.
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.
# Specify whether to overwrite an existing object with the same name during a multipart upload operation.
# If you do not specify x-oss-forbid-overwrite, the existing object is overwritten by default.
# If you set x-oss-forbid-overwrite to false, the existing object is allowed to be overwritten.
# If you set x-oss-forbid-overwrite to true, the existing object is not allowed to be overwritten. If an object with the same name exists, the program reports an error.
headers = {'x-oss-forbid-overwrite': 'true'}
bucket.complete_multipart_upload(key, upload_id, parts, headers=headers)
# 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 file copy, see CopyObject.
For more information about the API operations for multipart upload, see InitiateMultipartUpload and CompleteMultipartUpload.