Help Center> Object Storage Service> Python> Object-Related APIs (SDK for Python)> Uploading an Object - Append (SDK for Python)
Updated on 2024-04-23 GMT+08:00

Uploading an Object - Append (SDK for Python)

Function

This API uploads a file or folder to an existing OBS bucket. These files can be texts, images, videos, or any other type of files.

The AppendObject operation adds data to the end of an object in a specified bucket. If there is no object with the same key values in the bucket, a new object is created.

Restrictions

  • To upload an object, you must be the bucket owner or have the required permission (obs:object:PutObject in IAM or PutObject in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Configuring an Object Policy.
  • The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
  • Uploaded objects are stored in buckets. To upload an object to a bucket, you must have the write permission for the bucket. The name of each object in a bucket must be unique.
  • The latest modification time of the object is updated each time an upload is appended.
  • If you use SSE-C encryption, the encryption header you configure, such as x-obs-server-side-encryption, must be carried in each append upload.
  • If you use SSE-KMS encryption, the encryption header you configure, such as x-obs-server-side-encryption, only needs to be carried when the object is uploaded for the first time and no object with the same name exists in the bucket.
  • The size of each append upload cannot exceed 5 GB.
  • The maximum number of append writes for each appendable object is 10,000.
  • If the storage class is COLD (Archive), this API cannot be called.
  • If cross-region replication is configured for a bucket, this API cannot be called.
  • Objects cannot be appended to parallel file systems.

Method

ObsClient.appendObject(bucketName, objectKey, content, metadata, headers, progressCallback, autoClose)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

bucketName

str

Yes

Explanation:

Bucket name

Restrictions:

  • A bucket name must be unique across all accounts and regions.
  • A bucket name:
    • Must be 3 to 63 characters long and start with a digit or letter. Lowercase letters, digits, hyphens (-), and periods (.) are allowed.
    • Cannot be formatted as an IP address.
    • Cannot start or end with a hyphen (-) or period (.).
    • Cannot contain two consecutive periods (..), for example, my..bucket.
    • Cannot contain periods (.) and hyphens (-) adjacent to each other, for example, my-.bucket or my.-bucket.
  • If you repeatedly create buckets of the same name in the same region, no error will be reported and the bucket properties comply with those set in the first creation request.

Default value:

None

objectKey

str

Yes

Explanation:

Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path that does not contain the bucket name.

For example, if the address for accessing the object is examplebucket.obs.ap-southeast-1.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt.

Value range:

The value must contain 1 to 1,024 characters.

Default value:

None

content

AppendObjectContent

Yes

Explanation:

Content to be appended

Value range:

See Table 2.

Default value:

None

metadata

dict

No

Explanation:

Custom metadata to be appended. You can add a header starting with x-obs-meta- in the request to define metadata. The custom metadata will be returned in the response when you retrieve the object or query the object metadata.

Restrictions:

  • This parameter is valid only for the first append upload.
  • The custom metadata cannot exceed 8 KB. To measure the custom metadata, sum the number of bytes in the UTF-8 encoding of each key and value.
  • The custom metadata keys are case insensitive, but are stored in lowercase in OBS. The key values are case sensitive.
  • Both custom metadata keys and their values must conform to US-ASCII standards. If non-ASCII or unrecognizable characters are required, they must be encoded or decoded in URL or Base64 on the client, because the server does not perform such operations.

Default value:

None

headers

AppendObjectHeader

No

Explanation:

Headers in the request used for configuring the storage class, redundancy policy, and other basic information about the object

Restrictions:

This parameter is valid only for the first append upload.

Value range:

See Table 3.

Default value:

None

progressCallback

callable

No

Explanation:

Callback function for obtaining the upload progress

Default value:

None

NOTE:

This function contains the following parameters in sequence: number of uploaded bytes, total number of bytes, and used time (in seconds). For details about the sample code, see Obtaining the Upload Progress (SDK for Python).

autoClose

bool

No

Explanation:

Whether or not to automatically close data streams after the upload is complete

Value range:

True: The data flow is automatically closed.

False: The data flow is not automatically closed.

Default value:

True

Table 2 AppendObjectContent

Parameter

Type

Mandatory (Yes/No)

Description

content

str

or

readable object

No

Explanation:

Content to be appended

Value range:

  • A character string of object content
  • Readable object
  • Path of the file to be uploaded (isFile must be set to True.)
NOTE:

If content is a readable object that contains the read attribute, data can be read from content. Otherwise, the object content is a character string.

Default value:

None

position

int

or

str

Yes

Explanation:

Position where the object data is appended

Restrictions:

For an object to be appended, the value of position must be set to 0 when the object is uploaded for the first time. For the second append upload, the value of position should be set to the value of nextPosition returned in the response when the first upload is successful.

Value range:

An integer greater than or equal to 0, in bytes

Default value:

0

offset

int

or

str

No

Explanation:

Offset, in bytes. This parameter is required if the content for an append upload is a local file.

Value range:

An integer greater than or equal to 0, in bytes

Default value:

0

isFile

bool

No

Explanation:

Whether content indicates the file path.

Value range:

True: content indicates the file path.

False: content does not indicate the file path.

Default value:

False

Table 3 AppendObjectHeader

Parameter

Type

Mandatory (Yes/No)

Description

md5

str

No

Explanation:

Base64-encoded MD5 value of the content to be appended. It is used for the OBS server to verify data integrity.

Value range:

Base64-encoded 128-bit MD5 value of the request body calculated according to RFC 1864

Example: n58IG6hfM7vqI4K0vnWpog==

Default value:

None

acl

str

No

Explanation:

Pre-defined access control policies, which can be specified in the append upload request. For details about the ACL, see ACLs.

Restrictions:

This parameter can only be configured in the first request for append upload. The configurations specified in the first request will be used in subsequent requests by default.

Value range:

See Table 4.

Default value:

None

location

str

No

Explanation:

If the bucket is configured with website hosting, the request for obtaining the object can be redirected to another object in the bucket or an external URL.

The request is redirected to object anotherPage.html in the same bucket:

location:/anotherPage.html

The request is redirected to an external URL http://www.example.com/:

location:http://www.example.com/

OBS obtains the specified value from the header and stores it in the object metadata location.

Restrictions:

  • This parameter can only be configured in the first request for append upload. The configurations specified in the first request will be used in subsequent requests by default.
  • The value must start with a slash (/), http://, or https:// and cannot exceed 2 KB.
  • OBS only supports redirection for objects in the root directory of a bucket.

Default value:

None

contentType

str

No

Explanation:

MIME type of the object specified in the first append upload MIME type is a standard way of describing a data type and is used by the browser to decide how to display data.

Value range:

See What Is Content-Type (MIME)? (Python SDK)

Restrictions:

This parameter can only be configured in the first request for append upload. The configurations specified in the first request will be used in subsequent requests by default.

Default value:

If you do not specify contentType when uploading an object, the SDK determines the object type based on the suffix of the specified object name and automatically assigns a value to contentType.

contentLength

int

No

Explanation:

Length of the content to be appended

Restrictions:

  • The object size in a single upload ranges from 0 to 5 GB.
  • To upload files larger than 5 GB, multipart uploads should be used.

Default value:

If this parameter is not specified, OBS SDK for Python automatically calculates the size of the object.

sseHeader

SseCHeader

or

SseKmsHeader

No

Explanation:

Server-side encryption header

Restrictions:

This parameter can only be configured in the first request for append upload. The configurations specified in the first request will be used in subsequent requests by default.

Default value:

None

storageClass

str

No

Explanation:

Storage class of the object that can be specified in the append upload request

Restrictions:

This parameter can only be configured in the first request for append upload. The configurations specified in the first request will be used in subsequent requests by default.

Value range:

  • If the storage class is Standard, leave this parameter blank.
  • For details about the available storage classes, see Table 5.

Default value:

None

successActionRedirect

str

No

Explanation:

Address (URL) to which a successfully answered request is redirected

  • If the value is valid and the request is successful, OBS returns status code 303. Location in the returned results contains SuccessActionRedirect as well as the bucket name, object name, and object ETag.
  • If the value is invalid, OBS ignores this parameter. In such case, Location in the returned results indicates the object address, and OBS returns a status code based on whether the operation succeeds or fails.

Default value:

None

extensionGrants

list of ExtensionGrant

No

Explanation:

List of extension permissions that can be specified in the append upload request

Restrictions:

This parameter can only be configured in the first request for append upload. The configurations specified in the first request will be used in subsequent requests by default.

Value range:

See Table 8.

Default value:

The value specified in the first append upload request

expires

int

No

Explanation:

Lifecycle (starting from the last modification time of the object) that can be specified in the append upload request. Once the object expires, it is automatically deleted.

Restrictions:

  • This parameter can only be configured in the first request for append upload. The configurations specified in the first request will be used in subsequent requests by default.
  • This parameter can be configured only when uploading the object. It cannot be modified by calling a metadata modification API.

Value range:

An integer greater than or equal to 0, in days

Default value:

None

Table 4 HeadPermission

Constant

Description

PRIVATE

Private read/write

A bucket or object can only be accessed by its owner.

PUBLIC_READ

Public read and private write

If this permission is granted on a bucket, anyone can read the object list, multipart uploads, metadata, and object versions in the bucket.

If it is granted on an object, anyone can read the content and metadata of the object.

PUBLIC_READ_WRITE

Public read/write

If this permission is granted on a bucket, anyone can read the object list, multipart uploads, metadata, and object versions in the bucket, and can upload or delete objects, initiate multipart uploads, upload parts, merge parts, copy parts, and cancel multipart upload tasks.

If it is granted on an object, anyone can read the content and metadata of the object.

PUBLIC_READ_DELIVERED

Public read on a bucket as well as objects in the bucket

If this permission is granted on a bucket, anyone can read the object list, multipart tasks, metadata, and object versions, and read the content and metadata of objects in the bucket.

NOTE:

PUBLIC_READ_DELIVERED cannot be applied to objects.

PUBLIC_READ_WRITE_DELIVERED

Public read/write on a bucket as well as objects in the bucket

If this permission is granted on a bucket, anyone can read the object list, multipart uploads, metadata, and object versions in the bucket, and can upload or delete objects, initiate multipart upload tasks, upload parts, assemble parts, copy parts, and abort multipart uploads. You can also read the content and metadata of objects in the bucket.

NOTE:

PUBLIC_READ_WRITE_DELIVERED cannot be applied to objects.

BUCKET_OWNER_FULL_CONTROL

If this permission is granted on an object, only the bucket and object owners have the full control over the object. By default, if you upload an object to a bucket of any other user, the bucket owner does not have the permissions on your object. After you grant this policy to the bucket owner, the bucket owner can have full control over your object.

Table 5 StorageClass

Parameter

Type

Description

STANDARD

Standard storage class

Explanation:

Features low access latency and high throughput and is used for storing massive, frequently accessed (multiple times a month) or small objects (< 1 MB) requiring quick response.

WARM

Infrequent Access storage class

Explanation:

Used for storing data that is semi-frequently accessed (fewer than 12 times a year) but is instantly available when needed.

COLD

Archive storage class

Explanation:

Used for storing rarely accessed (once a year) data.

Table 6 SseCHeader

Parameter

Type

Mandatory (Yes/No)

Description

encryption

str

Yes

Explanation:

SSE-C used for encrypting objects

Value range:

AES256

Default value:

None

key

str

Yes

Explanation:

Key used in SSE-C encryption. It corresponds to the encryption method. For example, if encryption is set to AES256, the key is calculated using the AES-256 algorithm.

Value range:

The value must contain 32 characters.

Default value:

None

Table 7 SseKmsHeader

Parameter

Type

Mandatory (Yes/No)

Description

encryption

str

Yes

Explanation:

SSE-KMS used for encrypting objects

Value range:

kms

Default value:

None

key

str

No

Explanation:

Master key used in SSE-KMS

Value range:

The following two formats are supported:

  • regionID:domainID:key/key_id
  • key_id

In the preceding formats:

Default value:

  • If this parameter is not specified, the default master key will be used.
  • If there is no such a default master key, OBS will create one and use it by default.
Table 8 ExtensionGrant

Parameter

Type

Mandatory (Yes/No)

Description

granteeId

str

No

Explanation:

Account (domain) ID of the grantee

Value range:

To obtain the account ID, see How Do I Get My Account ID and IAM User ID? (SDK for Python)

Default value:

None

permission

str

No

Explanation:

Granted permissions

Default value:

None

Table 9 Permission

Constant

Description

READ

Read permission

A grantee with this permission for a bucket can obtain the list of objects, multipart uploads, bucket metadata, and object versions in the bucket.

A grantee with this permission for an object can obtain the object content and metadata.

WRITE

Write permission

A grantee with this permission for a bucket can upload, overwrite, and delete any object or part in the bucket.

Such permission for an object is not applicable.

READ_ACP

Permission to read ACL configurations

A grantee with this permission can obtain the ACL of a bucket or object.

A bucket or object owner has this permission for the bucket or object permanently.

WRITE_ACP

Permission to modify ACL configurations

A grantee with this permission can update the ACL of a bucket or object.

A bucket or object owner has this permission for the bucket or object permanently.

A grantee with this permission can modify the access control policy and thus the grantee obtains full access permissions.

FULL_CONTROL

Full control access, including read and write permissions for a bucket and its ACL, or for an object and its ACL.

A grantee with this permission for a bucket has READ, WRITE, READ_ACP, and WRITE_ACP permissions for the bucket.

A grantee with this permission for an object has READ, WRITE, READ_ACP, and WRITE_ACP permissions for the object.

Responses

Table 10 List of returned results

Type

Description

GetResult

Explanation:

SDK common results

Table 11 GetResult

Parameter

Type

Description

status

int

Explanation:

HTTP status code

Value range:

A status code is a group of digits ranging from 2xx (indicating successes) to 4xx or 5xx (indicating errors). It indicates the status of a response. For more information, see Status Code.

Default value:

None

reason

str

Explanation:

Reason description.

Default value:

None

errorCode

str

Explanation:

Error code returned by the OBS server. If the value of status is less than 300, this parameter is left blank.

Default value:

None

errorMessage

str

Explanation:

Error message returned by the OBS server. If the value of status is less than 300, this parameter is left blank.

Default value:

None

requestId

str

Explanation:

Request ID returned by the OBS server

Default value:

None

indicator

str

Explanation:

Error indicator returned by the OBS server.

Default value:

None

hostId

str

Explanation:

Requested server ID. If the value of status is less than 300, this parameter is left blank.

Default value:

None

resource

str

Explanation:

Error source (a bucket or an object). If the value of status is less than 300, this parameter is left blank.

Default value:

None

header

list

Explanation:

Response header list, composed of tuples. Each tuple consists of two elements, respectively corresponding to the key and value of a response header.

Default value:

None

body

object

Explanation:

Result content returned after the operation is successful. If the value of status is larger than 300, this parameter value is null. The value varies with the API being called. For details, see Bucket-Related APIs (SDK for Python) and Object-Related APIs (SDK for Python).

Default value:

None

Table 12 GetResult.body

GetResult.body Type

Description

AppendObjectResponse

Explanation:

Response to the append upload request. For details, see Table 13.

Table 13 AppendObjectResponse

Parameter

Type

Description

storageClass

str

Explanation:

Object storage class.

Value range:

  • If the storage class is Standard, leave this parameter blank.
  • For details about the available storage classes, see Table 5.

Default value:

None

etag

str

Explanation:

ETag of an object, which is a base64-encoded 128-bit MD5 digest. ETag is the unique identifier of the object content. It can be used to determine whether the object content is changed. For example, if the ETag is A when an object is uploaded and is B when the object is downloaded, the object content is changed. The ETag reflects changes only to the contents of the object, not its metadata. An uploaded object or copied object has a unique ETag.

Restrictions:

If an object is encrypted using server-side encryption, the ETag is not the MD5 value of the object.

Value range:

The value must contain 32 characters.

Default value:

None

nextPosition

int

Explanation:

Start position for next appending

Value range:

An integer greater than or equal to 0, in bytes

Default value:

None

sseKms

str

Explanation:

SSE-KMS algorithm

Value range:

kms

Default value:

None

sseKmsKey

str

Explanation:

ID of the KMS master key when SSE-KMS is used

Value range:

Valid value formats are as follows:

  1. regionID:domainID:key/key_id
  2. key_id

In the preceding formats:

Default value:

  • If this parameter is not specified, the default master key will be used.
  • If there is no such a default master key, the system will create one and use it by default.

sseC

str

Explanation:

SSE-C algorithm

Value range:

AES256

Default value:

None

sseCKeyMd5

str

Explanation:

MD5 value of the key for encrypting objects when SSE-C is used. This value is used to check whether any error occurs during the transmission of the key.

Restrictions:

The value is encrypted by MD5 and then encoded by Base64, for example, 4XvB3tbNTN+tIEVa0/fGaQ==.

Default value:

None

objectUrl

str

Explanation:

Full path to the object

Default value:

None

Code Examples

This example appends content to an object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from obs import ObsClient
from obs import AppendObjectContent
import os
import traceback

# Obtain an AK and SK pair using environment variables or import the AK and SK pair in other ways. Using hard coding may result in leakage.
# Obtain an AK and SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
ak = os.getenv("AccessKeyID")
sk = os.getenv("SecretAccessKey")
# (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them from environment variables.
security_token = os.getenv("SecurityToken")
# Set server to the endpoint corresponding to the bucket. Here uses CN-Hong Kong as an example. Replace it with the one in use.
server = "https://obs.ap-southeast-1.myhuaweicloud.com"

# Create an obsClient instance.
# If you use a temporary AK and SK pair and a security token to access OBS, you must specify security_token when creating an instance.
obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
try:
    # Specify the message body of the request for an append upload.
    content = AppendObjectContent()
    # Specify the content to be appended.
    content.content = 'Hello OBS'
    # Specify the position (0 as an example) where content is appended.
    content.position = 0
    # If you upload an object for the first time using the append upload, an error will be reported (status code 409) if an ordinary object with the same name already exists.
    bucketName = "examplebucket"
    # Specify the name of the object to append content to.
    objectKey = "objectname"
    # Append content to the object.
    resp = obsClient.appendObject(bucketName, objectKey, content)

    # If status code 2xx is returned, the API is called successfully. Otherwise, the API call fails.
    if resp.status < 300:
        print('Append Object Succeeded')
        print('requestId:', resp.requestId)
        print('nextPosition:', resp.body.nextPosition)
    else:
        print('Append Object Failed')
        print('requestId:', resp.requestId)
        print('errorCode:', resp.errorCode)
        print('errorMessage:', resp.errorMessage)
except:
    print('Append Object Failed')   
    print(traceback.format_exc())
  • Objects uploaded using ObsClient.putObject, referred to as common objects, can overwrite objects uploaded using ObsClient.appendObject, referred to as appendable objects. Data cannot be appended to an appendable object anymore once the object has been overwritten by a common object.
  • When you upload an object for the first time in appendable mode, an exception will be reported (HTTP status code 409) if a common object with the same name exists.
  • The ETag returned for the append upload is the ETag for the appended content, rather than that of the whole object.
  • Data appended each time can be up to 5 GB, and a maximum of 10,000 uploads can be appended for an object.
  • After an append upload is successful, you can obtain the location for the next append upload by using body.nextPosition in the returned results or call ObsClient.getObjectMetadata.