• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. API Reference (Python SDK)
  4. Objects-Related APIs
  5. PUT File

PUT File

API Description

You can use this API to upload a file or folder to a specified bucket.

Method Definition

ObsClient.putFile(bucketName, objectKey, file_path, metadata, headers)

Request Parameter

Field

Type

Optional or Mandatory

Description

bucketName

str

Mandatory

Bucket name

objectKey

str

Mandatory

Object name

file_path

str

Mandatory

Path to the file or folder to be uploaded

metadata

dict

Optional

Customized metadata of the object

headers

PutObjectHeader

Optional

Additional headers of the file

NOTE:

If file_path is a folder, contentLength, md5, and contentType in headers cannot take effect.

Returned Result

Type

Description

GetResult

SDK common result object

GetResult.body Type

Description

PutContentResponse

Response result of PUT Object

NOTE:

If file_path is a folder, the returned result is a list of GetResult.

Sample Code

try:
    from com.obs.models.put_object_header import PutObjectHeader 
     
    headers = PutObjectHeader() 
    headers.contentType = 'text/plain' 
     
    resp = obsClient.putFile('bucketname', 'objectkey', 'localfile', metadata={'meta1':'value1', 'meta2':'value2'}, headers=headers) 
          
    if resp.status < 300: 
        print('requestId:', resp.requestId) 
        print('etag:', resp.body.etag) 
        print('versionId:', resp.body.versionId) 
        print('storageClass:', resp.body.storageClass) 
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())