Updated on 2022-02-10 GMT+08:00

PUT Object

API Description

You can use this API to upload an object to a specified bucket.

Method Definition

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

Request Parameter

Field

Type

Optional or Mandatory

Description

Bucket

string

Mandatory

Bucket name

Key

string

Mandatory

Object name

ACL

string

Optional

Pre-defined access control policy specified during object creation

StorageClass

string

Optional

Storage class, which can be specified during the object creation

Body

string

or

resource

or

GuzzleHttp\Psr7\StreamInterface

Optional

Object content to be uploaded

SourceFile

string

Optional

Path to the source file of the object

Metadata

associative array

Optional

Customized metadata of the object

WebsiteRedirectLocation

string

Optional

Location where the object is redirected to, when the bucket is configured with website hosting.

ContentType

string

Optional

MIME type of the object

ContentLength

integer

Optional

Object size in bytes

ContentMD5

string

Optional

Base64-encoded MD5 value of the object data to be uploaded. It is provided for the OBS server to verify data integrity.

SseKms

string

Optional

Algorithm used in SSE-KMS encryption. The value can be:

  • kms

SseKmsKey

string

Optional

Master key used in SSE-KMS encryption. The value can be null.

SseC

string

Optional

Algorithm used in SSE-C encryption. The value can be:

  • AES256

SseCKey

string

Optional

Key used in SSE-C encryption. It is calculated by using AES-256.

  • Body and SourceFile cannot be used together.
  • If both Body and SourceFile are null, the size of the object to be uploaded is 0 bytes.

Returned Result

Field

Type

Description

HttpStatusCode

integer

HTTP status code

Reason

string

Reason description

RequestId

string

Request ID returned by the OBS server

ObjectURL

string

Object URL

ETag

string

Object ETag

VersionId

string

Object version ID

StorageClass

string

Storage class of the object. When the storage class is OBS Standard, the value is null.

SseKms

string

Algorithm used in SSE-KMS encryption

SseKmsKey

string

Key used in SSE-KMS encryption

SseC

string

Algorithm used in SSE-C encryption

SseCKeyMd5

string

MD5 value of the key used in SSE-C encryption

Sample Code

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