• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Python SDK)
  4. Object Download
  5. Performing a Streaming Download

Performing a Streaming Download

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
  
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

resp = obsClient.getObject('bucketname', 'objectkey', loadStreamInMemory=False)
if resp.status < 300:
    print('requestId:', resp.requestId)
    # Obtain the object content.
    while True:
        chunk = resp.body.response.read(65536)
        if not chunk:
            break
        print(chunk)
    resp.body.response.close()
else:
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)
NOTE:

If you perform a stream download in the condition that loadStreamInMemory is set to False, the body.response field in the returned result is a readable stream. You can read the object content and save the content to a local file or the memory through the stream.

Object input streams obtained by body.response must be closed explicitly. Otherwise, resource leakage occurs.