更新时间:2022-02-10 GMT+08:00

上传对象

功能说明

上传单个对象到指定桶中。

方法定义

1. ObsClient->putObject(array $parameter)
2. ObsClient->putObjectAsync(array $parameter, callable $callback)

请求参数

字段名

类型

约束

说明

Bucket

string

必选

桶名。

Key

string

必选

对象名。

ACL

string

可选

创建对象时可指定的预定义访问策略

StorageClass

string

可选

创建对象时可指定的对象的存储类型

Body

string

resource

GuzzleHttp\Psr7\StreamInterface

可选

待上传对象的内容。

SourceFile

string

可选

待上传对象的源文件路径。

Metadata

associative array

可选

待上传对象的自定义元数据。

WebsiteRedirectLocation

string

可选

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

ContentType

string

可选

待上传对象的MIME类型。

ContentLength

integer

可选

待上传对象数据的长度。

ContentMD5

string

可选

待上传对象数据的MD5值(经过Base64编码),提供给OBS服务端,校验数据完整性。

SseKms

string

可选

以SSE-KMS方式加密对象,支持的值:

  • kms

SseKmsKey

string

可选

SSE-KMS方式下加密的主密钥,可为空。

SseC

string

可选

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

  • AES256

SseCKey

string

可选

SSE-C方式下加密的密钥,由AES256算法得到。

  • Body与SourceFile不能同时使用。
  • 当Body与SourceFile都为空时,上传对象的大小为0字节。

返回结果

字段名

类型

说明

HttpStatusCode

integer

HTTP状态码。

Reason

string

HTTP文本描述。

RequestId

string

OBS服务端返回的请求ID。

ObjectURL

string

对象的地址。

ETag

string

对象的ETag值。

VersionId

string

对象的版本号。

StorageClass

string

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

SseKms

string

SSE-KMS方式的算法。

SseKmsKey

string

SSE-KMS方式的密钥。

SseC

string

SSE-C方式的算法。

SseCKeyMd5

string

SSE-C方式的密钥的MD5值。

代码样例

try{
       $resp = $obsClient -> putObject([
              'Bucket' => 'bucketname',
              'Key' => 'objectkey',
              'Metadata' => ['meta1' => 'value1', 'meta2' => 'value2'],
//            'SourceFile' => 'localfile',
              'Body' => 'Hello OBS',
              'ContentType' => 'text/plain'
       ]);
       printf("RequestId:%s\n", $resp['RequestId']);
       printf("VersionId:%s\n", $resp['VersionId']);
       printf("StorageClass:%s\n", $resp['StorageClass']);
       printf("ETag:%s\n", $resp['ETag']);
}catch (Obs\Common\ObsException $obsException){
       printf("ExceptionCode:%s\n", $obsException->getExceptionCode());
       printf("ExceptionMessage:%s\n", $obsException->getExceptionMessage());
}