• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Python 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:

resp = obsClient.copyObject('sourcebucketname', 'sourceobjectkey', 'destbucketname', 'destobjectkey')

if resp.status < 300:
    print('requestId:', resp.requestId)
else:
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)

Rewriting Object Properties

The following sample code shows how to rewrite object properties.

# Import the module.
from com.obs.client.obs_client import ObsClient
 
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)
from com.obs.models.copy_object_header import CopyObjectHeader
headers = CopyObjectHeader()
# Set the rewrite option of the object properties.
headers.directive = 'REPLACE'
headers.contentType = 'image/jpeg'
headers.storageClass = 'STANDARD'

# Rewrite the customized metadata.
metadata = {'property' : 'property-value'}

resp = obsClient.copyObject('sourcebucketname', 'sourceobjectkey', 'destbucketname', 'destobjectkey', headers=headers, metadata=metadata)

if resp.status < 300:
    print('requestId:', resp.requestId)
else:
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)
NOTE:

Use the metadata parameter to specify the object's customized metadata to be rewritten and the CopyObjectHeader.directive parameter to specify the rewrite mode, which can be REPLACE (rewrite) or COPY (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 error code will be returned and the copy will fail.

You can set the following conditions:

Parameter

Description

Format

CopyObjectHeader.if_modified_since

Copies the source object if it is changed after the time specified by this parameter; otherwise, an error code is returned.

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

CopyObjectHeader.if_unmodified_since

Copies the source object if it is changed before the time specified by this parameter; otherwise, an error code is returned.

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

CopyObjectHeader.if_match

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

Character string

CopyObjectHeader.if_none_match

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

Character string

NOTE:
  • The ETag of the source object is the MD5 check value of the source object.
  • If if_unmodified_sinceif_matchif_modified_since, or if_none_match is included, and the specified condition is not met, error code 412 Precondition Failed, will be returned.
  • if_modified_since and if_none_match can be used together. So do if_unmodified_since and if_match.

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
 
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)
from com.obs.models.copy_object_header import CopyObjectHeader
headers = CopyObjectHeader()
headers.if_modified_since = 'Thu, 31 Dec 2015 16:00:00 GMT'
headers.if_none_match = 'none-match-etag'

resp = obsClient.copyObject('sourcebucketname', 'sourceobjectkey', 'destbucketname', 'destobjectkey', headers=headers)

if resp.status < 300:
    print('requestId:', resp.requestId)
else:
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)

Rewriting an Object ACL

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
 
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)
from com.obs.models.copy_object_header import CopyObjectHeader
headers = CopyObjectHeader()
# Rewrite the object ACL to public-read during the copy.
headers.acl = 'public-read'

resp = obsClient.copyObject('sourcebucketname', 'sourceobjectkey', 'destbucketname', 'destobjectkey', headers=headers)

if resp.status < 300:
    print('requestId:', resp.requestId)
else:
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)