Uploading Objects - Streaming

API Description

You can use this API to upload readable objects that contain the read attribute as the data source and upload data to a specified bucket in network stream or file stream mode.

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 or the name of the uploaded object

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 an object

progressCallback

callable

Optional

Callback function for obtaining the upload progress

NOTE:

This function contains the following parameters in sequence: number of uploaded bytes, total number of bytes, and used time (unit: second). For details about the sample code, see Uploading an Object – Obtaining the Upload Progress.

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

Uploading a Network Stream
try:
    import sys
    if sys.version_info.major == 2 or not sys.version > '3':
        import httplib
    else:
        import http.client as httplib

    conn = httplib.HTTPConnection('www.a.com', 80)
    conn.request('GET', '/')
    content = conn.getresponse()

    resp = obsClient.putContent('bucketname', 'objectname', content=content)

    if resp.status < 300:    
        print('requestId:', resp.requestId)
    else:    
        print('errorCode:', resp.errorCode)
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())

Uploading a File Stream
try:
    content = open('localfile', 'rb')

    resp = obsClient.putContent('bucketname', 'objectname', content=content)

    if resp.status < 300:    
        print('requestId:', resp.requestId)
    else:    
        print('errorCode:', resp.errorCode)
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())
  • When this upload mode is used, the value of content must be a readable object that contains the read property.
  • When uploading file streams, you must open files in rb or rb+ mode.
  • To upload a large file, you are advised to use multipart upload.
  • The content to be uploaded cannot exceed 5 GB.