
# 下载对象
#### 功能说明
下载指定桶中的对象。
#### 方法定义
```
1. ObsClient->getObject(array $parameter)
2. ObsClient->getObjectAsync(array $parameter, callable $callback)
```
#### 请求参数
| **字段名**                    | **类型**                                                                         | **约束** | **说明**                                                                                                                                                                              |
|:---|:---|:---|:---|
| Bucket                     | string                                                                         | 必选     | 桶名。                                                                                                                                                                                 |
| Key                        | string                                                                         | 必选     | 对象名。                                                                                                                                                                                |
| VersionId                  | string                                                                         | 可选     | 对象的版本号。                                                                                                                                                                             |
| IfMatch                    | string                                                                         | 可选     | 如果对象的ETag值与该参数值相同，则返回对象内容，否则抛出异常。                                                                                                                                                   |
| IfModifiedSince            | string 或 \\DateTime       | 可选     | 如果对象的修改时间晚于该参数值指定的时间，则返回对象内容，否则抛出异常。该参数值为字符串时必须符合http://www.ietf.org/rfc/rfc2616.txt规定的HTTP时间格式。                                                                                    |
| IfNoneMatch                | string                                                                         | 可选     | 如果对象的ETag值与该参数值不相同，则返回对象内容，否则抛出异常。                                                                                                                                                  |
| IfUnmodifiedSince          | string 或 \\DateTime | 可选     | 如果对象的修改时间早于该参数值指定的时间，则返回对象内容，否则抛出异常。该参数值为字符串时必须符合http://www.ietf.org/rfc/rfc2616.txt规定的HTTP时间格式。                                                                                    |
| Range                      | string                                                                         | 可选     | 指定下载的范围，取值区间：\[0，对象长度-1\]，格式：bytes=x-y。如果Range的最大长度超出对象长度-1，仍旧取对象长度-1。                                                                                                              |
| Origin                     | string                                                                         | 可选     | 预请求指定的跨域请求Origin（通常为域名）。                                                                                                                                                            |
| RequestHeader              | string                                                                         | 可选     | 跨域请求可以使用的HTTP头域。                                                                                                                                                                    |
| ResponseCacheControl       | string                                                                         | 可选     | 重写响应中的Cache-Control头。                                                                                                                                                               |
| ResponseContentDisposition | string                                                                         | 可选     | 重写响应中的Content-Disposition头。                                                                                                                                                         |
| ResponseContentEncoding    | string                                                                         | 可选     | 重写响应中的Content-Encoding头。                                                                                                                                                            |
| ResponseContentLanguage    | string                                                                         | 可选     | 重写响应中的Content-Language头。                                                                                                                                                            |
| ResponseContentType        | string                                                                         | 可选     | 重写响应中的Content-Type头。 常见的contentType（MIME）列表参见[配置、编辑、查看对象元数据](https://support.huaweicloud.com/usermanual-obs/obs_03_0324.html)。                             |
| ResponseExpires            | string                                                                         | 可选     | 重写响应中的Expires头。                                                                                                                                                                     |
| ImageProcess               | string                                                                         | 可选     | 图片处理参数。                                                                                                                                                                             |
| SaveAsFile                 | string                                                                         | 可选     | 下载对象的目标路径，包含文件名。                                                                                                                                                                    |
| SaveAsStream               | boolean                                                                        | 可选     | 是否将对象以数据流的形式返回。                                                                                                                                                                     |
| FilePath                   | string                                                                         | 可选     | 废弃参数，与旧版本保持兼容。下载对象的目标路径，包含文件名。                                                                                                                                                      |
| SseC                       | string                                                                         | 可选     | 以SSE-C方式解密对象，支持的值： - AES256：使用SSE-C方式完成加解密，且使用AES256算法。   |
| SseCKey                    | string                                                                         | 可选     | SSE-C方式下解密的密钥，由AES256算法算出。 SSE-C方式下解密的密钥，该密钥需要您自定义，并由256bit的密钥经过base64编码得到。                                                                          |
   
![](https://support.huaweicloud.com/api-obs_php_sdk_api_zh/public_sys-resources/note_3.0-zh-cn.png)
- 当SaveAsStream为true时不能与SaveAsFile或FilePath一起使用。
- SaveAsFile与FilePath不能一起使用。
- 如果包含IfUnmodifiedSince并且不符合或者包含IfMatch并且不符合，抛出异常中HTTP状态码为：412 precondition failed。
- 如果包含IfModifiedSince并且不符合或者包含IfNoneMatch并且不符合，抛出异常中HTTP状态码为：304 Not Modified。
 
#### 返回结果
| **字段名**                 | **类型**                   | **说明**                                                                                                                     |
|:---|:---|:---|
| HttpStatusCode          | integer                  | HTTP状态码。                                                                                                                   |
| Reason                  | string                   | HTTP文本描述。                                                                                                                  |
| RequestId               | string                   | OBS服务端返回的请求ID。                                                                                                             |
| DeleteMarker            | boolean                  | 标识删除的对象是否是删除标记。                                                                                                            |
| LastModified            | string                   | 对象的最近一次修改时间。                                                                                                               |
| ContentLength           | integer                  | 对象数据的长度。                                                                                                                   |
| CacheControl            | string                   | 响应中的Cache-Control头。                                                                                                        |
| ContentDisposition      | string                   | 响应中的Content-Disposition头。                                                                                                  |
| ContentEncoding         | string                   | 响应中的Content-Encoding头。                                                                                                     |
| ContentLanguage         | string                   | 响应中的Content-Language头。                                                                                                     |
| ContentType             | string                   | 对象的MIME类型，常见的contentType（MIME）列表参见[配置、编辑、查看对象元数据](https://support.huaweicloud.com/usermanual-obs/obs_03_0324.html)。        |
| Expires                 | string                   | 响应中的Expires头。                                                                                                              |
| ETag                    | string                   | 对象的ETag值。                                                                                                                  |
| VersionId               | string                   | 对象的版本号。                                                                                                                    |
| WebsiteRedirectLocation | string                   | 当桶设置了Website配置，该参数指明对象的重定向地址。                                                                                              |
| StorageClass            | string                   | 对象的存储类型，当对象存储类型是STANDARD时，该值为空。                                                                                            |
| Restore                 | string                   | 归档存储类型对象的恢复状态。                                                                                                             |
| AllowOrigin             | string                   | 如果请求中的Origin满足桶的CORS规则，则返回CORS规则中的AllowedOrigin。                                                                           |
| AllowHeader             | string                   | 如果请求的RequestHeader满足桶的CORS规则，则返回CORS规则中的AllowedHeader。                                                                     |
| AllowMethod             | string                   | 桶CORS规则中的AllowedMethod。                                                                                                    |
| ExposeHeader            | string                   | 桶CORS规则中的ExposeHeader。                                                                                                     |
| MaxAgeSeconds           | string                   | 桶CORS规则中的MaxAgeSeconds。                                                                                                    |
| SseKms                  | string                   | SSE-KMS方式的算法。                                                                                                              |
| SseKmsKey               | string                   | SSE-KMS加密方式下使用的KMS主密钥的ID值，为空则默认配置为默认密钥。密钥ID获取方法请参见[查看密钥](https://support.huaweicloud.com/usermanual-dew/dew_01_0179.html)。 |
| SseC                    | string                   | SSE-C方式的算法。                                                                                                                |
| SseCKeyMd5              | string                   | SSE-C方式的密钥的MD5值。                                                                                                           |
| Expiration              | string                   | 对象的详细过期信息。                                                                                                                 |
| Body                    | GuzzleHttp\\Psr7\\Stream | 对象的内容。当设置了SaveAsFile时该值为空；当设置了SaveAsStream且为true时该值为可读流，需要调用GuzzleHttp\\Psr7\\Stream-\>read方法读取数据。                         |
| SaveAsFile              | string                   | 下载对象的目标路径，包含文件名，与请求中的该参数对应。                                                                                                |
| Metadata                | associative array        | 对象自定义元数据。                                                                                                                  |
   
#### 代码样例
```
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());
}
```
