更新时间:2025-12-11 GMT+08:00

下载对象

功能说明

下载指定桶中的对象。

方法定义

ObsClient.getObject

请求参数

字段名

类型

约束

说明

Bucket

String

必选

桶名。

Key

String

必选

对象名。

RequestDate

String

或

Date

可选

指定请求时间。

说明:

当为String类型时,必须符合ISO8601或RFC822规范。

VersionId

String

可选

对象的版本号。

IfMatch

String

可选

如果对象的ETag值与该参数值相同,则返回对象内容,否则返回异常码。

IfModifiedSince

String

可选

如果对象的修改时间晚于该参数值指定的时间,则返回对象内容,否则返回异常码。该参数值必须符合http://www.ietf.org/rfc/rfc2616.txt规定的HTTP时间格式。

IfNoneMatch

String

可选

如果对象的ETag值与该参数值不相同,则返回对象内容,否则返回异常码。

IfUnmodifiedSince

String

可选

如果对象的修改时间早于该参数值指定的时间,则返回对象内容,否则返回异常码。该参数值必须符合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头,当SaveByType为file的时候不支持该参数,可使用临时url接口。

ResponseContentEncoding

String

可选

重写响应中的Content-Encoding头。

ResponseContentLanguage

String

可选

重写响应中的Content-Language头。

ResponseContentType

String

可选

重写响应中的Content-Type头,常见的contentType(MIME)列表参见配置、编辑、查看对象元数据。

ResponseExpires

String

可选

重写响应中的Expires头。

ImageProcess

String

可选

图片处理参数。

SaveByType

String

可选

下载对象的方式,支持的值:

  • text:文本下载
  • arraybuffer:二进制下载
  • blob:范围下载
  • file:文件下载

ProgressCallback

Function

可选

获取下载进度的回调函数。

说明:

该回调函数依次包含三个参数:已下载的字节数、总字节数、已使用的时间(单位:秒)。

SseC

String

可选

以SSE-C方式解密对象,支持的值:

  • AES256

SseCKey

String

可选

SSE-C方式下解密的密钥,该密钥需要您自定义,并由256bit的密钥经过base64编码得到。

  • 如果包含IfUnmodifiedSince并且不符合或者包含IfMatch并且不符合,抛出异常中HTTP状态码为:412 precondition failed。
  • 如果包含IfModifiedSince并且不符合或者包含IfNoneMatch并且不符合,抛出异常中HTTP状态码为:304 Not Modified。

返回结果(InterfaceResult)

字段名

类型

说明

RequestId

String

OBS服务端返回的请求ID。

DeleteMarker

String

标识删除的对象是否是删除标记。

LastModified

String

对象的最近一次修改时间。

ContentLength

String

对象数据的长度。

CacheControl

String

响应中的Cache-Control头。

ContentDisposition

String

响应中的Content-Disposition头。

ContentEncoding

String

响应中的Content-Encoding头

ContentLanguage

String

响应中的Content-Language头

ContentType

String

对象的MIME类型,常见的contentType(MIME)列表参见配置、编辑、查看对象元数据。

Expires

String

响应中的Expires头。

ETag

String

对象的ETag值。

VersionId

String

对象的版本号。

WebsiteRedirectLocation

String

当桶设置了Website配置,该参数指明对象的重定向地址。

StorageClass

String

对象的存储类型,当对象存储类型是标准存储时,该值为空。

Restore

String

归档存储类型对象的恢复状态。

Expiration

String

对象的详细过期信息。

Content

String

或

ArrayBuffer

或

Blob

或

Object

对象的内容。当SaveByType未设置或者为text时该值为String对象;当SaveByType为arraybuffer时该值为ArrayBuffer对象;当SaveByType为blob时该值为Blob对象;当SaveByType为file时该值为包含下载该对象的URL(有效期3600秒)的Object对象。

Metadata

Object

对象自定义元数据。需要在桶的CORS配置中增加允许响应中可返回的附加头域。例如,新增x-amz-meta-property1以获取自定义元数据property1。

代码样例

obsClient.getObject({
       Bucket : 'bucketname',
       Key : 'objectkey',
       Range: 'bytes=0-10',
       SaveByType: 'file'
},function (err, result) {
       if(err){
              console.error('Error-->' + err);
       }else{
              if(result.CommonMsg.Status < 300){
                     console.log('RequestId-->' + result.InterfaceResult.RequestId);
                     console.log('ETag-->' + result.InterfaceResult.ETag);
                     console.log('VersionId-->' + result.InterfaceResult.VersionId);
                     console.log('ContentLength-->' + result.InterfaceResult.ContentLength);
                     console.log('DeleteMarker-->' + result.InterfaceResult.DeleteMarker);
                     console.log('LastModified-->' + result.InterfaceResult.LastModified);
                     console.log('StorageClass-->' + result.InterfaceResult.StorageClass);
                     console.log('Content-->' + result.InterfaceResult.Content);
                     console.log('Metadata-->' + JSON.stringify(result.InterfaceResult.Metadata));       
              }else{
                     console.log('Code-->' + result.CommonMsg.Code);
                     console.log('Message-->' + result.CommonMsg.Message);
              }
       }
});