Creating a Folder

API Description

You can use this API to upload a folder to a specified bucket. There is no folder in its real meaning in OBS. All elements in 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 (/). Such objects have no difference from other objects and can be downloaded and deleted, except that they are displayed as folders in OBS Console.

Method Definition

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

Request Parameters

Field

Type

Optional or Mandatory

Description

bucketName

str

Mandatory

Bucket name

objectKey

str

Mandatory

Object name

content

str

or

readable object

Optional

Object content to be uploaded

metadata

dict

Optional

Customized metadata of the object

headers

PutObjectHeader

Optional

Additional header of the request for uploading objects

progressCallback

callable

Optional

Callback function for obtaining the upload progress

NOTE:

This callback function contains the following parameters in sequence: number of uploaded bytes, total bytes, and used time (unit: second).

Streaming upload, file-based upload, multipart upload, appendable upload, and resumable upload are supported.

autoClose

bool

Optional

After the upload is complete, data flow is automatically closed. The default value is True.

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.

Returned Results

Type

Description

GetResult

SDK common result object

GetResult.body Type

Description

PutContentResponse

Response result of the request for uploading an object

Sample Code

try:
    resp = obsClient.putContent('bucketname', 'parent_directory/', content=None)
    if resp.status < 300: 
        print('requestId:', resp.requestId) 
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)
    
    # Create an object in the folder.
    resp = obsClient.putContent('bucketname', 'parent_directory/objectname', content='Hello OBS') 
          
    if resp.status < 300: 
        print('requestId:', resp.requestId) 
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())