Tous les produits
Search
Centre de documentation

Object Storage Service:Empêcher l'écrasement d'objets portant le même nom à l'aide du SDK OSS pour Python 1.0

Dernière mise à jour :Sep 22, 2026

Par défaut, si vous téléchargez un objet portant le même nom qu'un objet existant, ce dernier est écrasé. Cette rubrique explique comment configurer l'en-tête de requête x-oss-forbid-overwrite pour empêcher l'écrasement des objets existants lors de la copie d'objets ou de l'exécution d'un téléchargement simple ou multipartie.

Notes

  • Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.

  • Les identifiants d'accès utilisés dans cette rubrique sont obtenus à partir des variables d'environnement. Pour plus d'informations sur la configuration des identifiants d'accès, consultez Configurer les identifiants d'accès à l'aide du SDK OSS pour Python 1.0.

  • Cette rubrique illustre la création d'une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification avec des identifiants provenant du Security Token Service (STS), consultez Initialisation.

Empêcher l'écrasement lors d'un téléchargement simple

L'exemple de code suivant montre comment empêcher l'écrasement des objets existants lors d'un téléchargement simple :

# -*- 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 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Upload the object. 
# Specify whether the object that is uploaded by calling the PutObject operation overwrites the existing object that has the same name. 
# By default, if x-oss-forbid-overwrite is not specified, the object that is uploaded overwrites the existing object that has the same name. 
# If x-oss-forbid-overwrite is set to false, the object that is uploaded overwrites the existing object that has the same name. 
# If x-oss-forbid-overwrite is set to true, the object that is uploaded cannot overwrite the existing object that has the same name. If an object that has the same name exists in the bucket, OSS returns an error. 
headers = {'x-oss-forbid-overwrite': 'true'}
result = bucket.put_object('yourObjectName', 'content of object', headers=headers)

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

Empêcher l'écrasement lors de la copie d'un objet

  • Copier un petit objet

    L'exemple de code suivant montre comment copier un petit objet sans écraser l'objet existant portant le même nom :

    # -*- 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 your bucket.
    bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName')
    
    # Specify whether the object that is copied by calling the copy_object operation overwrites the existing object that has the same name. 
    # By default, if x-oss-forbid-overwrite is not specified, an existing object is overwritten by a copied object with the same name. 
    # If x-oss-forbid-overwrite is set to false, an existing object is overwritten by a copied object with the same name. 
    # If x-oss-forbid-overwrite is set to true, an existing object is not overwritten by a copied object with the same name. If the existing object has the same name as the copied object, an error is reported. 
    headers = dict()
    headers = {'x-oss-forbid-overwrite':'true'}
    bucket.copy_object('yourSourceBucketName', 'yourSourceObjectName', 'yourDestinationObjectName', headers=headers)
  • Copier un grand objet

    L'exemple de code suivant montre comment empêcher l'écrasement d'un objet existant par un grand objet portant le même nom lors de la copie de ce dernier à l'aide d'une copie multipartie :

    # -*- 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 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 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. 
    endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
    # Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
    region = "cn-hangzhou"
    
    # Specify the name of your 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)
    
    # Initiate a multipart upload task. 
    # Specify whether to overwrite an object that has the same name when you copy an object. 
    # By default, if x-oss-forbid-overwrite is not specified, the object that is copied overwrites the existing object that has the same name. 
    # If x-oss-forbid-overwrite is set to false, the object that is copied overwrites the existing object that has the same name. 
    # If x-oss-forbid-overwrite is set to true, an existing object is not overwritten by a copied object with the same name. If the existing object has the same name as the copied object, an error is reported. 
    headers = dict()
    headers = {'x-oss-forbid-overwrite': 'true'}
    upload_id = bucket.init_multipart_upload(dst_object, headers=headers).upload_id
    parts = []
    
    # Copy each part. 
    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 task. 
    # Specify whether to overwrite an object that has the same name when you copy an object. 
    # By default, if x-oss-forbid-overwrite is not specified, the object that is uploaded overwrites the existing object that has the same name. 
    # If x-oss-forbid-overwrite is set to false, the object that is uploaded overwrites the existing object that has the same name. 
    # If x-oss-forbid-overwrite is set to true, an existing object is not overwritten by a copied object with the same name. If the existing object has the same name as the copied object, an error is reported. 
    headers = dict()
    headers = {'x-oss-forbid-overwrite':'true'}
    bucket.complete_multipart_upload(dst_object, upload_id, parts, headers=headers)

Empêcher l'écrasement lors d'un téléchargement multipartie

L'exemple de code suivant montre comment empêcher l'écrasement d'un objet existant par un objet portant le même nom lors du téléchargement de cet objet à l'aide d'un téléchargement multipartie :

# -*- 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 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 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

key = 'yourObjectName'
filename = 'yourLocalFile'

total_size = os.path.getsize(filename)
# Specify the determine_part_size method to determine the part size. 
part_size = determine_part_size(total_size, preferred_size=100 * 1024)

# Initiate a multipart upload task. 
# Specify whether to overwrite the existing object that has the same name when you perform multipart upload. 
# By default, if x-oss-forbid-overwrite is not specified, the object that is uploaded overwrites the existing object that has the same name. 
# If x-oss-forbid-overwrite is set to false, the object that is uploaded overwrites the existing object that has the same name. 
# If x-oss-forbid-overwrite is set to true, the object that is uploaded cannot overwrite the existing object that has the same name. If an object that has the same name exists in the bucket, OSS returns an error. 
headers = {'x-oss-forbid-overwrite': 'true'}
upload_id = bucket.init_multipart_upload(key, headers=headers).upload_id
parts = []

# Upload the parts. 
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 object and recalculates the position from which the append operation starts. 
        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 task. 
# Specify whether to overwrite the existing object that has the same name when you perform multipart upload. 
# By default, if x-oss-forbid-overwrite is not specified, the object that is uploaded overwrites the existing object that has the same name. 
# If x-oss-forbid-overwrite is set to false, the object that is uploaded overwrites the existing object that has the same name. 
# If x-oss-forbid-overwrite is set to true, the object that is uploaded cannot overwrite the existing object that has the same name. If an object that has the same name exists in the bucket, OSS returns an error. 
headers = {'x-oss-forbid-overwrite': 'true'}
bucket.complete_multipart_upload(key, upload_id, parts, headers=headers)

# Verify the result of the multipart upload task. 
with open(filename, 'rb') as fileobj:
    assert bucket.get_object(key).read() == fileobj.read()

Références

  • Pour plus d'informations sur l'opération API permettant d'effectuer un téléchargement simple, consultez PutObject.

  • Pour plus d'informations sur l'opération API permettant de copier un objet, consultez CopyObject.

  • Pour plus d'informations sur les opérations API permettant d'effectuer un téléchargement multipartie, consultez InitiateMultipartUpload et CompleteMultipartUpload.