Uploading an Object - File-Based (SDK for Python)
Function
This API uploads local files to OBS over the Internet. These files can be texts, images, videos, or any other type of files.
- OBS does not involve folders like in a file system. All elements stored in OBS buckets are objects. To create a folder in OBS is essentially to create an object whose size is 0 and whose name ends with a slash (/). You can perform download, delete, or other operations on such objects as you do on ordinary objects.
- If versioning is enabled, when uploading an object, OBS automatically allocates a unique version ID for the object. Objects with the same name are stored in OBS as objects with different version IDs. If versioning is not enabled, when uploading an object to a folder where there is already an object with the same name, the new object will overwrite the existing one.
- You can pass user-defined headers in extensionHeaders in a dictionary. For details, see User-defined Header (SDK for Python).
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 object size in a single upload ranges from 0 to 5 GB.
- To upload files larger than 5 GB, multipart uploads should be used.
Method
ObsClient.putFile(bucketName, objectKey, file_path, metadata, headers, progressCallback)
Request Parameters
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
bucketName |
str |
Yes |
Explanation: Bucket name Restrictions:
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.eu-west-101.myhuaweicloud.eu/folder/test.txt, the object name is folder/test.txt. Value range: The value must contain 1 to 1,024 characters. Default value: None |
file_path |
str |
Yes |
Explanation: Full path of the file or folder to be uploaded, for example, aa/bb.txt or aa/. Default value: None
NOTE:
If file_path is a folder, contentLength, md5, and contentType in headers cannot take effect. |
metadata |
dict |
No |
Explanation: Custom metadata of the object to be uploaded. 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:
Default value: None |
headers |
No |
Explanation: Headers in the request used for configuring the storage class, redundancy policy, and other basic information about the object Value range: See Table 2. Default value: None |
|
progressCallback |
callable |
No |
Explanation: Callback function for obtaining the upload progress Restrictions: The progress of a folder upload cannot be obtained. 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). Upload progress callback only supports streaming, file-based, multipart, appendable, and resumable uploads. |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
md5 |
str |
No |
Explanation: Base64-encoded MD5 value of the data to be uploaded. 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 policy specified during object creation. For details about the ACL, see ACLs. Value range: See Table 3. 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:
Default value: None |
contentType |
str |
No |
Explanation: MIME type of the file to be uploaded. 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) 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: Size of the object to be uploaded Restrictions:
Default value: If this parameter is not specified, OBS SDK for Python automatically calculates the size of the object. |
sseHeader |
or |
No |
Explanation: Server-side encryption header Default value: None |
storageClass |
str |
No |
Explanation: Storage class of the object Value range: See Table 4. Default value: None |
successActionRedirect |
str |
No |
Explanation: Address (URL) to which a successfully answered request is redirected
Default value: None |
extensionGrants |
list of ExtensionGrant |
No |
Explanation: List of the extended permissions for the object to be uploaded Value range: See Table 7. Default value: None |
crc64 |
int or str or long |
No |
Explanation: CRC64 value of the data to be uploaded. Restrictions: This parameter is not supported for POSIX or SFS objects. Value range: A 64-bit CRC value calculated based on the ECMA-182 standard. Default value: None |
isAttachCrc64 |
bool |
No |
Explanation: Whether to automatically calculate the CRC64 value of the data to be uploaded. Restrictions: This parameter is not supported for POSIX or SFS objects. Value range:
Default value: False |
expires |
int |
No |
Explanation: Expiration time of the object (calculated from the latest modification time of the object). Expired objects are automatically deleted. Restrictions: 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 |
Constant |
Default Value |
Description |
---|---|---|
HeadPermission.PRIVATE |
private |
Private read/write A bucket or object can only be accessed by its owner. |
HeadPermission.PUBLIC_READ |
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. |
HeadPermission.PUBLIC_READ_WRITE |
public-read-write |
Public read/write If this permission is granted on a bucket, anyone can read the object list, multipart tasks, 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 upload tasks. If it is granted on an object, anyone can read the content and metadata of the object. |
HeadPermission.PUBLIC_READ_DELIVERED |
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. |
HeadPermission.PUBLIC_READ_WRITE_DELIVERED |
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. They can also read the content and metadata of objects in the bucket.
NOTE:
PUBLIC_READ_WRITE_DELIVERED cannot be applied to objects. |
HeadPermission.BUCKET_OWNER_FULL_CONTROL |
public-read-write-delivered |
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. |
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. |
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 |
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:
In the preceding formats:
Default value:
|
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 |
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, READ_ACP, and WRITE_ACP permissions for the object. |
Responses
Type |
Description |
---|---|
Explanation: SDK common results |
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 is left blank. 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 |
GetResult.body Type |
Description |
---|---|
Explanation: Response to the request for uploading an object. For details, see Table 12. Default value: None |
If file_path is a folder, the returned result is a GetResult list.
Parameter |
Type |
Description |
---|---|---|
storageClass |
str |
Explanation: Storage class of the object Value range:
Default value: None |
versionId |
str |
Explanation: Object version ID. If versioning is enabled for the bucket, the object version ID will be returned. Value range: The value must contain 32 characters. 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 |
crc64 |
str |
Explanation: A 64-bit CRC value calculated based on the ECMA-182 standard. It uniquely identifies an object and can be used to check the integrity of the object content. If an object has different CRC64 values when being uploaded and downloaded, its content has been changed. CRC64 reflects changes to the contents of the object, not its metadata. Restrictions:
Value range: A 64-bit CRC value calculated based on the ECMA-182 standard. 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:
In the preceding formats:
Default value:
|
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 uploads a file.
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 45 46 |
from obs import ObsClient from obs import PutObjectHeader 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/eu/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 EU-Dublin as an example. Replace it with the one in use. server = "https://obs.eu-west-101.myhuaweicloud.eu" # 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 additional headers of the request for uploading an object. headers = PutObjectHeader() # (Optional) Specify the MIME type of the object. headers.contentType = 'text/plain' bucketName = "examplebucket" # Specify an object name (the name displayed after the file is uploaded to the bucket). objectKey = "objectname" # Specify the full path of the file or folder to be uploaded, for example, aa/bb.txt or aa/. file_path = 'localfile' # Specify the custom metadata of the object. metadata = {'meta1': 'value1', 'meta2': 'value2'} # Perform the file-based upload. resp = obsClient.putFile(bucketName, objectKey, file_path, metadata, headers) # If status code 2xx is returned, the API is called successfully. Otherwise, the API call fails. if resp.status < 300: print('Put File Succeeded') print('requestId:', resp.requestId) print('etag:', resp.body.etag) print('versionId:', resp.body.versionId) print('storageClass:', resp.body.storageClass) else: print('Put File Failed') print('requestId:', resp.requestId) print('errorCode:', resp.errorCode) print('errorMessage:', resp.errorMessage) except: print('Put File Failed') print(traceback.format_exc()) |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.