• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (PHP SDK)
  4. Object Management
  5. Copying an Object

Copying an Object

The object copy operation can create a copy for an existing object in OBS.

You can call ObsClient->copyObject to copy an object. When copying an object, you can rewrite properties and ACL for it, as well as set restriction conditions.

Copying an Object in Simple Mode

Sample code:

// Import the third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
      'key' => '*** Provide your Access Key ***',
      'secret' => '*** Provide your Secret Key ***',
      'endpoint' => 'https://yourdomainname'
] );

$resp = $obsClient->copyObject ( [ 
      'Bucket' => 'destbucketname',
      'Key' => 'destobjectkey',
      'CopySource' => 'sourcebucketname/sourceobjectkey'
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );
NOTE:

Use the CopySource parameter to specify the information about the source object.

Rewriting Object Properties

The following sample code shows how to rewrite object properties.

// Import the third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
] );

$resp = $obsClient->copyObject ( [ 
       'Bucket' => 'destobjectkey',
       'Key' => 'destobjectkey',
       'CopySource' => 'sourcebucketname/soureobjectkey',
       'ContentType' => 'image/jpeg',
       'StorageClass' => ObsClient::StorageClassWarm,
       'Metadata' => ['property' => 'property-value'],
       'MetadataDirective' => ObsClient::ReplaceMetadata
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );
NOTE:

Use the Metadata parameter to specify the object's customized metadata to be rewritten and the MetadataDirective parameter to specify the rewrite mode, which can be ObsClient::ReplaceMetadata (rewrite) or ObsClient::CopyMetadata (copy from the source object).

Copying an Object by Specifying Conditions

When copying an object, you can specify one or more restriction conditions. If the conditions are met, the object will be copied. Otherwise, an exception will be thrown.

You can set the following conditions:

Parameter

Description

Format

CopySourceIfModifiedSince

Copies the source object if it is changed after the time specified by this parameter; otherwise, an exception is thrown.

This parameter must conform to the HTTP time format specified in http://www.ietf.org/rfc/rfc2616.txt.

CopySourceIfUnmodifiedSince

Copies the source object if it is changed before the time specified by this parameter; otherwise, an exception is thrown.

This parameter must conform to the HTTP time format specified in http://www.ietf.org/rfc/rfc2616.txt.

CopySourceIfMatch

Copies the source object if its ETag is the same as the one specified by this parameter; otherwise, an exception is thrown.

Character string

CopySourceIfNoneMatch

Copies the source object if its ETag is different from the one specified by this parameter; otherwise, an exception is thrown.

Character string

NOTE:
  • The ETag of the source object is the MD5 check value of the source object.
  • If the object copy request includes CopySourceIfUnmodifiedSinceCopySourceIfMatchCopySourceIfModifiedSince, or CopySourceIfNoneMatch, and the specified condition is not met, the copy will fail and an exception will be thrown with HTTP status code 412 Precondition Failed returned.
  • CopySourceIfModifiedSince and CopySourceIfNoneMatch can be used together. So do CopySourceIfUnmodifiedSince and CopySourceIfMatch.

Sample code:

// Import the third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
] );

$resp = $obsClient->copyObject ( [ 
       'Bucket' => 'destobjectkey',
       'Key' => 'destobjectkey',
       'CopySource' => 'sourcebucketname/soureobjectkey',
       'CopySourceIfModifiedSince' => 'Thu, 31 Dec 2015 16:00:00 GMT',    
       'CopySourceIfNoneMatch' => 'none-match-etag'
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );

Rewriting an Object ACL

Sample code:

// Import the third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
] );

$resp = $obsClient->copyObject ( [ 
       'Bucket' => 'destobjectkey',
       'Key' => 'destobjectkey',
       'CopySource' => 'sourcebucketname/soureobjectkey',
       // Rewrite the object ACL to public-read.
       'ACL' => ObsClient::AclPublicRead
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );
NOTE:

Use the ACL parameter to modify the object ACL.