Help Center/ Object Storage Service/ SDK Reference/ Python/ Object-Related APIs (SDK for Python)/ Downloading an Object - Range-Based (SDK for Python)
Updated on 2024-11-26 GMT+08:00

Downloading an Object - Range-Based (SDK for Python)

Function

This API downloads partial data of an object by specifying a range. If the specified range is from 0 to 1,000, data from byte 0 to byte 1,000, 1,001 bytes in total, are returned. If the specified range is invalid, data of the whole object is returned.

Restrictions

  • To download an object, you must be the bucket owner or have the required permission (obs:object:GetObject in IAM or GetObject in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Configuring an Object Policy.
  • The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
  • Objects in the Archive storage class can be downloaded only when they are restored.
  • If the specified range is invalid (for example, the start or end position is set to a negative integer or the range is larger than the object length), the whole object is returned.
  • This download method also can be used to concurrently download parts of a large object. For details about the sample code, see concurrent_download_object_sample.

Method

ObsClient.getObject(bucketName, objectKey, downloadPath, getObjectRequest, headers, loadStreamInMemory, progressCallback, extensionHeaders)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

bucketName

str

Yes

Explanation:

Bucket name

Restrictions:

  • A bucket name must be unique across all accounts and regions.
  • A bucket name:
    • Must be 3 to 63 characters long and start with a digit or letter. Lowercase letters, digits, hyphens (-), and periods (.) are allowed.
    • Cannot be formatted as an IP address.
    • Cannot start or end with a hyphen (-) or period (.).
    • Cannot contain two consecutive periods (..), for example, my..bucket.
    • Cannot contain periods (.) and hyphens (-) adjacent to each other, for example, my-.bucket or my.-bucket.
  • If you repeatedly create buckets of the same name in the same region, no error will be reported and the bucket properties comply with those set in the first creation request.

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.ap-southeast-1.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt.

Value range:

The value must contain 1 to 1,024 characters.

Default value:

None

downloadPath

str

No

Explanation:

The download path with the file name contained, for example, aa/bb.txt.

Default value:

None

getObjectRequest

GetObjectRequest

No

Explanation:

Additional parameters of an object download request

Value range:

See Table 2.

headers

GetObjectHeader

No

Explanation:

Additional headers of an object download request

Value range:

See Table 3.

loadStreamInMemory

bool

No

Explanation:

Whether to load the data stream of the object to memory

Value range:

True: The obtained data stream is loaded to memory with downloadPath ignored.

False: The obtained data stream is not loaded to memory.

Default value:

False

progressCallback

callable

No

Explanation:

Callback function for obtaining the download progress

Default value:

None

NOTE:

This function contains the following parameters in sequence: number of downloaded bytes, total number of bytes, and used time (in seconds). For details about the sample code, see Downloading an Object - Obtaining the Download Progress (SDK for Python).

extensionHeaders

dict

No

Explanation:

Extension headers.

Value range:

See User-defined Header (SDK for Python).

Default value:

None

Table 2 GetObjectRequest

Parameter

Type

Mandatory (Yes/No)

Description

cache_control

str

No

Explanation:

Cache-Control is rewritten in the response.

Default value:

None

content_disposition

str

No

Explanation:

Content-Disposition is rewritten in the response.

Default value:

None

content_encoding

str

No

Explanation:

Content-Encoding is rewritten in the response.

Default value:

None

content_language

str

No

Explanation:

Content-Language is rewritten in the response.

Default value:

None

content_type

str

No

Explanation:

Content-Type is rewritten in the response.

Default value:

None

expires

str

No

Explanation:

Expires is rewritten in the response.

Default value:

None

versionId

str

No

Explanation:

Object version ID, for example, G001117FCE89978B0000401205D5DC9

Value range:

The value must contain 32 characters.

Default value:

None. If this parameter is left blank, the latest version of the object is obtained.

imageProcess

str

No

Explanation:

Image processing command or style. Example for resizing and rotating an image: image/resize,m_fixed,w_100,h_100/rotate,90

Value range:

Command format: image/commands

Style format: style/style name

For details about image processing parameters, see Processing Images.

Default value:

If no commands are entered, the original image will be returned.

Table 3 GetObjectHeader

Parameter

Type

Mandatory (Yes/No)

Description

range

str

No

Explanation:

Download range

Value range:

0 to the object length minus 1. Format: x-y

Restrictions:

The upper limit of range is the length of the object minus 1. If the specified value exceeds this limit, the length of the object minus 1 is used.

Default value:

None

if_match

str

No

Explanation:

Preset ETag. If the ETag of the object to be downloaded is the same as the preset ETag, the object is returned. Otherwise, an error is returned.

Value range:

The value must contain 32 characters.

Default value:

None

if_none_match

str

No

Explanation:

Preset ETag. If the ETag of the object to be downloaded is different from the preset ETag, the object is returned. Otherwise, an error is returned.

Value range:

The value must contain 32 characters.

Default value:

None

if_modified_since

str

or

DateTime

No

Explanation:

The object is returned if it has been modified since the specified time; otherwise, an error is returned.

Restrictions:

You can generate a date time by referring to Table 4.

For example, DateTime(year=2023, month=9, day=12)

Default value:

None

if_unmodified_since

str

or

DateTime

No

Explanation:

The object is returned if it has not been modified since the specified time; otherwise, an error is returned.

Restrictions:

You can generate a date time by referring to Table 4.

For example, DateTime(year=2023, month=9, day=12)

Default value:

None

origin

str

No

Explanation:

Origin of the cross-domain request specified by the preflight request. Generally, it is a domain name.

Restrictions:

Each origin can contain only one wildcard character (*).

Default value:

None

requestHeaders

str

No

Explanation:

HTTP headers in a cross-origin request. Only CORS requests matching the allowed headers are valid.

Restrictions:

Each header can contain only one wildcard character (*). Spaces, ampersands (&), colons (:), and less-than signs (<) are not allowed.

Default value:

None

sseHeader

SseCHeader

No

Explanation:

Server-side decryption headers. For details, see Table 5.

Restrictions:

If the object uploaded to the server is encrypted on the server using the encryption key provided by the client, downloading the object requires including the encryption key in the message.

Default value:

None

Table 4 DateTime

Parameter

Type

Description

year

int

Explanation:

Year in UTC

Default value:

None

month

int

Explanation:

Month in UTC

Default value:

None

day

int

Explanation:

Day in UTC

Default value:

None

hour

int

Explanation:

Hour in UTC

Restrictions:

The value is in 24-hour format.

Default value:

0

min

int

Explanation:

Minute in UTC

Default value:

0

sec

int

Explanation:

Second in UTC

Default value:

0

Table 5 SseCHeader

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

List of returned results

Table 6 List of returned results

Type

Description

GetResult

Explanation:

SDK common results

Table 7 GetResult

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, the value of body is null. 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

Table 8 GetResult.body

GetResult.body Type

Description

ObjectStream

Explanation:

Response to the request for downloading an object

Table 9 ObjectStream

Parameter

Type

Description

response

object

Explanation:

If loadStreamInMemory is set to False and downloadPath is left blank in the request, this parameter is returned and indicates a readable stream. You can read object content from it.

buffer

object

Explanation:

If loadStreamInMemory is set to True in the request, this parameter is returned and indicates the data stream in the memory.

size

int

Explanation:

If loadStreamInMemory is set to True in the request, this parameter is returned and indicates the size of the data stream.

Value range:

0 to (263 – 1), in bytes

url

str

Explanation:

If loadStreamInMemory is set to False and downloadPath is not left blank, this parameter is returned and indicates the download path.

deleteMarker

bool

Explanation:

Whether the deleted object is a delete marker

Value range:

  • True: The deleted object is a delete marker.
  • False: The deleted object is not a delete marker.

Default value:

False

storageClass

str

Explanation:

Object storage class.

Value range:

  • If the storage class is Standard, leave this parameter blank.
  • For details about the available storage classes, see Table 10.

Default value:

None

accessContorlAllowOrigin

str

Explanation:

If Origin in the request meets the CORS rules of the bucket, AllowedOrigin specified in the CORS rules is returned. AllowedOrigin indicates the origin from which the requests can access the bucket.

Restrictions:

Domain name of the origin. Each origin can contain only one wildcard character (*), for example, https://*.vbs.example.com.

Default value:

None

accessContorlAllowHeaders

str

Explanation:

If RequestHeader in the request meets the CORS rules of the bucket, AllowedHeader specified in the CORS rules is returned. AllowedHeader indicates the allowed headers for cross-origin requests. Only CORS requests matching the allowed headers are valid.

Restrictions:

Each header can contain only one wildcard character (*). Spaces, ampersands (&), colons (:), and less-than signs (<) are not allowed.

Default value:

None

accessContorlAllowMethods

str

Explanation:

AllowedMethod in the CORS rules of the bucket. It specifies the HTTP method of cross-origin requests, that is, the operation type of buckets and objects.

Value range:

The following HTTP methods are supported:

  • GET
  • PUT
  • HEAD
  • POST
  • DELETE

Default value:

None

accessContorlExposeHeaders

str

Explanation:

ExposeHeader in the CORS rules of the bucket. It specifies the CORS-allowed additional headers in the response. These headers provide additional information to clients. By default, your browser can only access headers Content-Length and Content-Type. If your browser needs to access other headers, add them to a list of the allowed additional headers.

Restrictions:

Spaces, wildcard characters (*), ampersands (&), colons (:), and less-than signs (<) are not allowed.

Default value:

None

accessContorlMaxAge

int

Explanation:

MaxAgeSeconds in the CORS rules of the bucket. It specifies the time your client can cache the response for a cross-origin request.

Restrictions:

Each CORS rule can contain only one MaxAgeSeconds.

Value range:

0 to (231 – 1), in seconds

Default value:

100

contentLength

int

Explanation:

Object size

Value range:

The value ranges from 0 TB to 48.8 TB, in bytes.

Default value:

None

cacheControl

str

Explanation:

Cache-Control header in the response

Default value:

None

contentDisposition

str

Explanation:

Content-Disposition header in the response

Default value:

None

contentEncoding

str

Explanation:

Content-Encoding header in the response

Default value:

None

contentLanguage

str

Explanation:

Content-Language header in the response

Default value:

None

contentType

str

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:

None

expires

str

Explanation:

Expires header in the response

Default value:

None

lastModified

str

Explanation:

Time when the last modification was made to the object

Restrictions:

The time must be in the GMT format, for example, Wed, 25 Mar 2020 02:39:52 GMT.

Default value:

None

etag

str

Explanation:

Base64-encoded, 128-bit MD5 value of an object. ETag is the unique identifier of the object contents and is used to determine whether the contents of an object are changed. For example, if the ETag value is A when an object is uploaded and is B when the object is downloaded, this indicates the contents of the object are changed. The ETag reflects changes only to the contents of an object, not its metadata. Objects created by the upload and copy operations have unique ETags after being encrypted using MD5.

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

versionId

str

Explanation:

Object version ID.

Value range:

The value must contain 32 characters.

Default value:

None

restore

str

Explanation:

Restore status of an object. This header is returned when an Archive object is being restored or has been restored.

For example, ongoing-request="true" indicates that the object is being restored. ongoing-request="false", expiry-date="Wed, 7 Nov 2012 00:00:00 GMT" indicates that the object has been restored. expiry-date indicates when the restored object expires.

Restrictions:

If the object is not in the Archive storage class, this parameter is left blank.

Default value:

None

expiration

str

Explanation:

Expiration details. Example: "expiry-date=\"Mon, 11 Sep 2023 00:00:00 GMT\""

Default value:

None

sseKms

str

Explanation:

Objects are encrypted using SSE-KMS on the server side.

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:

  1. regionID:domainID:key/key_id
  2. key_id

In the preceding formats:

Default value:

  • If this parameter is not specified, the default master key will be used.
  • If there is no such a default master key, the system will create one and use it by default.

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

websiteRedirectLocation

str

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. This parameter specifies the address the request for the object is redirected to.

The request is redirected to object anotherPage.html in the same bucket:

WebsiteRedirectLocation:/anotherPage.html

The request is redirected to an external URL http://www.example.com/:

WebsiteRedirectLocation:http://www.example.com/

OBS obtains the specified value from the header and stores it in the object metadata WebsiteRedirectLocation.

Restrictions:

  • The value must start with a slash (/), http://, or https:// and cannot exceed 2 KB.
  • OBS only supports redirection for objects in the root directory of a bucket.

Default value:

None

Table 10 StorageClass

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.

Code Examples

This example downloads part of object objectname by specifying a range from 0 to 1,000.

 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
from obs import GetObjectHeader
from obs import ObsClient
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/intl/en-us/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. CN-Hong Kong is used here as an example. Replace it with the one in use.
server = "https://obs.ap-southeast-1.myhuaweicloud.com" 

# 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 downloading an object.
    headers = GetObjectHeader()
    # Specify the range (0-1000 as an example) of the object bytes to be returned.
    headers.range = '0-1000'
    bucketName = "examplebucket"
    objectKey = "objectname"
    # Perform the range download. If loadStreamInMemory is set to True, downloadpath will be invalid, and data streams will be downloaded to the memory.
    resp = obsClient.getObject(bucketName, objectKey, loadStreamInMemory=True, headers=headers)
    # If status code 2xx is returned, the API is called successfully. Otherwise, the API call fails.
    if resp.status < 300:
        print('Get Object Succeeded')
        print('requestId:', resp.requestId)
        # Obtain the object content.
        print('buffer:', resp.body.buffer)
    else:
        print('Get Object Failed')
        print('requestId:', resp.requestId)
        print('errorCode:', resp.errorCode)
        print('errorMessage:', resp.errorMessage)
except: 
    print('Get Object Failed')
    print(traceback.format_exc())
  • If the specified range is invalid (for example, the start or end position is set to a negative integer or the range is larger than the object length), the whole object is returned.
  • This download method also can be used to concurrently download parts of a large object. For details about the sample code, see concurrent_download_object_sample.