Copying an Object
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see API Reference.
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.
- If the source object to be copied is in the Archive storage class, you must restore it first.
Copying an Object in Simple Mode
Sample code:
// Import the dependency library. require 'vendor/autoload.php'; // Import the SDK code library during source code installation. // require 'obs-autoloader.php'; // Declare the namespace. use Obs\ObsClient; // Create an instance of ObsClient. $obsClient = new ObsClient ( [ //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage. //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. 'key' => getenv('ACCESS_KEY_ID'), 'secret' => getenv('SECRET_ACCESS_KEY'), 'endpoint' => 'https://your-endpoint', 'signature' => 'obs' ] ); $resp = $obsClient->copyObject ( [ 'Bucket' => 'destbucketname', 'Key' => 'destobjectname', 'CopySource' => 'sourcebucketname/sourceobjectname' ] ); printf ( "RequestId:%s\n", $resp ['RequestId'] );
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 dependency library. require 'vendor/autoload.php'; // Import the SDK code library during source code installation. // require 'obs-autoloader.php'; // Declare the namespace. use Obs\ObsClient; // Create an instance of ObsClient. $obsClient = new ObsClient ( [ //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage. //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. 'key' => getenv('ACCESS_KEY_ID'), 'secret' => getenv('SECRET_ACCESS_KEY'), 'endpoint' => 'https://your-endpoint', 'signature' => 'obs' ] ); $resp = $obsClient->copyObject ( [ 'Bucket' => 'destobjectname', 'Key' => 'destobjectname', 'CopySource' => 'sourcebucketname/soureobjectname', 'ContentType' => 'image/jpeg', 'StorageClass' => ObsClient::StorageClassWarm, 'Metadata' => ['property' => 'property-value'], 'MetadataDirective' => ObsClient::ReplaceMetadata ] ); printf ( "RequestId:%s\n", $resp ['RequestId'] );
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 has been modified since the specified time; 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 has not been modified since the specified time; 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 |
- The ETag of the source object is the MD5 check value of the source object.
- If the object copy request includes CopySourceIfUnmodifiedSince, CopySourceIfMatch, CopySourceIfModifiedSince, 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 dependency library. require 'vendor/autoload.php'; // Import the SDK code library during source code installation. // require 'obs-autoloader.php'; // Declare the namespace. use Obs\ObsClient; // Create an instance of ObsClient. $obsClient = new ObsClient ( [ //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage. //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. 'key' => getenv('ACCESS_KEY_ID'), 'secret' => getenv('SECRET_ACCESS_KEY'), 'endpoint' => 'https://your-endpoint', 'signature' => 'obs' ] ); $resp = $obsClient->copyObject ( [ 'Bucket' => 'destobjectname', 'Key' => 'destobjectname', 'CopySource' => 'sourcebucketname/soureobjectname', '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 dependency library. require 'vendor/autoload.php'; // Import the SDK code library during source code installation. // require 'obs-autoloader.php'; // Declare the namespace. use Obs\ObsClient; // Create an instance of ObsClient. $obsClient = new ObsClient ( [ //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage. //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. 'key' => getenv('ACCESS_KEY_ID'), 'secret' => getenv('SECRET_ACCESS_KEY'), 'endpoint' => 'https://your-endpoint', 'signature' => 'obs' ] ); $resp = $obsClient->copyObject ( [ 'Bucket' => 'destobjectname', 'Key' => 'destobjectname', 'CopySource' => 'sourcebucketname/soureobjectname', // Rewrite the object ACL to public-read. 'ACL' => ObsClient::AclPublicRead ] ); printf ( "RequestId:%s\n", $resp ['RequestId'] );
Use the ACL parameter to modify the object ACL.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot