更新时间:2026-01-15 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类型,常见的contentType(MIME)列表参见。

ContentLength

integer

可选

待上传对象数据的长度。

ContentMD5

string

可选

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

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

返回结果

字段名

类型

说明

HttpStatusCode

integer

HTTP状态码。

Reason

string

HTTP文本描述。

RequestId

string

OBS服务端返回的请求ID。

ObjectURL

string

对象的地址。

ETag

string

对象的ETag值。

VersionId

string

对象的版本号。

StorageClass

string

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

代码样例

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());
}