Downloading an Object
API Description
You can use this API to download an object in a specified bucket.
Method Definition
1. ObsClient->getObject(array $parameter) 2. ObsClient->getObjectAsync(array $parameter, callable $callback)
Request Parameter
| Field | Type | Optional or Mandatory | Description |
|---|---|---|---|
| Bucket | string | Mandatory | Bucket name |
| Key | string | Mandatory | Object name |
| VersionId | string | Optional | Object version ID |
| IfMatch | string | Optional | Returns the source object if its ETag is the same as the one specified by this parameter; otherwise, an exception is thrown. |
| IfModifiedSince | string or \DateTime | Optional | Returns the object if it is modified after the time specified by this parameter; otherwise, an exception is thrown. If this parameter value is a character string, it must conform to the HTTP time format specified in http://www.ietf.org/rfc/rfc2616.txt. |
| IfNoneMatch | string | Optional | Returns the source object if its ETag is different from the one specified by this parameter; otherwise, an exception is thrown. |
| IfUnmodifiedSince | string or \DateTime | Optional | Returns the object if it remains unchanged since the time specified by this parameter; otherwise, an exception is thrown. If this parameter value is a character string, it must conform to the HTTP time format specified in http://www.ietf.org/rfc/rfc2616.txt. |
| Range | string | Optional | Download range. The value range is [0, object length-1] and is in the format of bytes = x-y. The maximum length of Range is the length of the object minus 1. If it exceeds this value, the length of the object minus 1 is used. |
| Origin | string | Optional | Origin of the cross-domain request specified by the pre-request. Generally, it is a domain name. |
| RequestHeader | string | Optional | HTTP header in a cross-domain request |
| ResponseCacheControl | string | Optional | Rewrites the Cache-Control header in the response. |
| ResponseContentDisposition | string | Optional | Rewrites the Content-Disposition header in the response. |
| ResponseContentEncoding | string | Optional | Rewrites the Content-Encoding header in the response. |
| ResponseContentLanguage | string | Optional | Rewrites the Content-Language header in the response. |
| ResponseContentType | string | Optional | Rewrites the Content-Type header in the response. For details about the common content types, see Configuring, Editing, and Viewing Object Metadata. |
| ResponseExpires | string | Optional | Rewrites the Expires header in the response. |
| SaveAsFile | string | Optional | Target path to which the object is downloaded (containing the file name) |
| SaveAsStream | boolean | Optional | Whether to return the object in the format of data stream |
| FilePath | string | Optional | Target path to which the object is downloaded (containing the file name). This is a deprecated parameter and is used to maintain compatibility with earlier versions. |
| SseC | string | Optional | Algorithm used in SSE-C decryption. The value can be:
|
| SseCKey | string | Optional | Custom key used in SSE-C decryption, which is calculated by using AES-256. The value is a Base64-encoded 256-bit key. |
- If SaveAsStream is true, it cannot be used with SaveAsFile or FilePath.
- SaveAsFile and FilePath cannot be used together.
- If the download request includes IfUnmodifiedSince or IfMatch and IfUnmodifiedSince or IfMatch is not met, an exception will be thrown with HTTP status code 412 Precondition Failed.
- If the download request includes IfModifiedSince or IfNoneMatch and IfModifiedSince or IfNoneMatch is not met, an exception will be thrown with HTTP status code 304 Not Modified.
Returned Result
| Field | Type | Description |
|---|---|---|
| HttpStatusCode | integer | HTTP status code |
| Reason | string | Reason description |
| RequestId | string | Request ID returned by the OBS server |
| DeleteMarker | boolean | Whether the deleted object is a delete marker |
| LastModified | string | Time when the last modification was made to the object |
| ContentLength | integer | Object size in bytes |
| CacheControl | string | Cache-Control header in the response |
| ContentDisposition | string | Content-Disposition header in the response |
| ContentEncoding | string | Content-Encoding header in the response |
| ContentLanguage | string | Content-Language header in the response |
| ContentType | string | MIME type of the object. For details about the common content types, see Configuring, Editing, and Viewing Object Metadata. |
| Expires | string | Expires header in the response |
| ETag | string | Object ETag |
| VersionId | string | Object version ID |
| WebsiteRedirectLocation | string | Location where the object is redirected to, when the bucket is configured with website hosting. |
| StorageClass | string | Storage class of the object. When the storage class is STANDARD, the value is null. |
| Restore | string | Restoration status of the object in the Archive storage class. |
| AllowOrigin | string | If Origin in the request meets the CORS rules of the bucket, AllowedOrigin in the CORS rules is returned. |
| AllowHeader | string | If RequestHeader in the request meets the CORS rules of the bucket, AllowedHeader in the CORS rules is returned. |
| AllowMethod | string | AllowedMethod in the CORS rules of the bucket |
| ExposeHeader | string | ExposeHeader in the CORS rules of the bucket |
| MaxAgeSeconds | string | MaxAgeSeconds in the CORS rules of the bucket |
| SseKms | string | Algorithm used in SSE-KMS decryption |
| SseKmsKey | string | ID of the KMS master key used in SSE-KMS encryption. If this parameter is left blank, the default key is used. To obtain the key ID, see Viewing a Key. |
| SseC | string | Algorithm used in SSE-C decryption |
| SseCKeyMd5 | string | MD5 value of the key used in SSE-C decryption |
| Expiration | string | Expiration details |
| Body | GuzzleHttp\Psr7\Stream | Object content. If SaveAsFile is set, this field is null. If SaveAsStream is set to true, this field is a readable stream. You need to call the GuzzleHttp\Psr7\Stream->read method to read the data. |
| SaveAsFile | string | Target path to which the object is downloaded (containing the file name), which is consistent with that set in the request |
| Metadata | associative array | Customized metadata of the object |
Sample Code
try{ $resp = $obsClient -> getObject([ 'Bucket' => 'bucketname', 'Key' => 'objectkey', // 'SaveAsFile' => 'localfile', // 'SaveAsStream' => true, 'Range' => 'bytes=0-10' ]); printf("RequestId:%s\n", $resp['RequestId']); printf("ETag:%s\n", $resp['ETag']); printf("VersionId:%s\n", $resp['VersionId']); printf("StorageClass:%s\n", $resp['StorageClass']); printf("ContentLength:%s\n", $resp['ContentLength']); printf("DeleteMarker:%s\n", $resp['DeleteMarker']); printf("LastModified:%s\n", $resp['LastModified']); printf("Body:%s\n", $resp['Body']); printf("Metadata:%s\n", print_r($resp['Metadata'], true)); }catch (Obs\Common\ObsException $obsException){ printf("ExceptionCode:%s\n", $obsException->getExceptionCode()); printf("ExceptionMessage:%s\n", $obsException->getExceptionMessage()); }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.