Tous les produits
Search
Centre de documentation

Object Storage Service:Téléchargement par ajout (OSS SDK for Python 1.0)

Dernière mise à jour :Aug 18, 2026

Appelez l'opération AppendObject pour ajouter du contenu à des objets existants de type « appendable ».

Notes

  • Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Pour 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.

  • Cette rubrique explique comment créer une instance OSSClient avec un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), reportez-vous à la section Initialisation.

  • Si l'objet auquel vous souhaitez ajouter du contenu n'existe pas, l'opération AppendObject crée un objet de type « appendable ».

  • Si l'objet auquel vous souhaitez ajouter du contenu existe déjà :

    • S'il s'agit d'un objet de type « appendable » et que la position spécifiée pour le début de l'ajout correspond à la taille actuelle de l'objet, les données sont ajoutées à la fin de l'objet.

    • S'il s'agit d'un objet de type « appendable » mais que la position spécifiée ne correspond pas à la taille actuelle de l'objet, l'erreur PositionNotEqualToLength est renvoyée.

    • Si l'objet n'est pas de type « appendable », l'erreur ObjectNotAppendable est renvoyée.

Permissions

Par défaut, un compte Alibaba Cloud dispose de toutes les autorisations. Les utilisateurs RAM ou les rôles RAM associés à un compte Alibaba Cloud ne disposent d'aucune autorisation par défaut. Le propriétaire du compte ou l'administrateur doit accorder les autorisations nécessaires via des politiques RAM ou une Bucket Policy.

API

Action

Description

AppendObject

oss:PutObject

Cette opération permet de télécharger un objet en l'ajoutant à un objet existant.

oss:PutObjectTagging

Lors du téléchargement d'un objet par ajout à un objet existant, cette autorisation est requise si vous spécifiez des tags d'objet via x-oss-tagging.

Exemples

L'exemple de code suivant montre comment effectuer un téléchargement par ajout :

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

# Obtain access credentials from environment variables. Before you run the 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)

# The following code provides an example on how to specify headers for the append upload: 
# headers = dict()
# Specify the caching behavior of the web page for the object. 
# headers['Cache-Control'] = 'no-cache'
# Specify the name of the object when the object is downloaded. 
# headers['Content-Disposition'] = 'oss_MultipartUpload.txt'
# Specify the request header that is used to check whether the content of the received message is the same as the content of the sent message. 
# headers['Content-MD5'] = 'ohhnqLBJFiKkPSBO1eNaUA=='
# Specify the expiration time. 
# headers['Expires'] = 'Wed, 08 Jul 2022 16:57:01 GMT'
# Specify the access control list (ACL) of the object. In this example, the ACL is set to OBJECT_ACL_PRIVATE. 
# headers['x-oss-object-acl'] = oss2.OBJECT_ACL_PRIVATE
# Specify whether to overwrite the object with the same name in the append upload. 
# headers['x-oss-forbid-overwrite'] = 'true'
# Specify the server-side encryption method. In this example, SSE-OSS is specified for server-side encryption. 
# headers[OSS_SERVER_SIDE_ENCRYPTION] = SERVER_SIDE_ENCRYPTION_AES256
# Specify the storage class of the object. 
# headers['x-oss-storage-class'] = oss2.BUCKET_STORAGE_CLASS_STANDARD
# You can add parameters whose names are prefixed with x-oss-meta- when you call the AppendObject operation to create an appendable object. These parameters cannot be included in the requests when you append content to an existing appendable object. Parameters whose names are prefixed with x-oss-meta- are considered the metadata of the object. 
# headers['x-oss-meta-author'] = 'Alice'
# result = bucket.append_object(exampledir/exampleobject.txt, 0, 'content of first append', headers=headers)

# Set the position from which the first append operation starts to 0. 
# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
result = bucket.append_object('exampledir/exampleobject.txt', 0, 'content of first append')
# If you have appended content to the object, you can obtain the position from which the current append operation starts from the next_position field in the response returned by the last operation or by using bucket.head_object. 
bucket.append_object('<yourObjectName>', result.next_position, 'content of second append')        

Références

  • Pour consulter l'exemple de code complet relatif au téléchargement par ajout, rendez-vous sur GitHub.

  • Pour plus d'informations sur l'opération API permettant d'effectuer un téléchargement par ajout, consultez la documentation relative à l'opération AppendObject.