This topic describes how to use the CopyObject method of OSS SDK for Python version 2.0 to copy an object that is less than 5 GiB in size from a source bucket to a destination bucket in the same region. The destination bucket can be the source bucket or a different bucket.
Notes
The sample code in this topic uses the region ID
cn-hangzhouof the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use the internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.To copy an object, you must have the read permissions on the source object and read and write permissions on the destination bucket.
The source bucket and destination bucket must be located in the same region. For example, objects in a bucket located in the China (Hangzhou) region cannot be copied to another bucket located in the China (Qingdao) region.
Make sure that no retention policies are configured for the source bucket and the destination bucket. Otherwise, the following error message is returned: The object you specified is immutable.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket Policy.
API | Action | Definition |
CopyObject |
| Copies objects within a bucket or between buckets in the same region. |
| ||
| If you specify the source object version through versionId, this permission is also required. | |
| If you copy object tags through x-oss-tagging, these permissions are required. | |
| ||
| If you specify the tags of a specific version of the source object through versionId, this permission is also required. | |
| When copying an object, if the destination object metadata contains X-Oss-Server-Side-Encryption: KMS, these two permissions are required. | |
|
Method
copy_object(request: CopyObjectRequest, **kwargs) → CopyObjectResultRequest parameters
Parameter | Type | Description |
request | CopyObjectRequest | The request parameter. For more information, see CopyObjectRequest. |
Common parameters of CopyObjectRequest
Parameter | Type | Description |
bucket | str | The name of the destination bucket. |
key | str | The name of the destination object. |
source_bucket | str | The name of the source bucket. |
source_key | str | The name of the source object. |
forbid_overwrite | str | Specifies whether the CopyObject operation overwrites an existing object that has the same name. |
tagging | str | The tags of the destination object. You can configure multiple tags for the destination object. Example: TagA=A&TagB=B. |
tagging_directive | str | The method that is used to configure tags for the destination object. Valid values:
|
Response parameters
Type | Description |
CopyObjectResult | The return value. For more information, see CopyObjectResult |
For the complete definition of the copy object method, see copy_object.
Sample code
The following sample code provides an example on how to copy an object that is less than 5 GiB in size from a source bucket to a destination bucket:
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line parameter parser.
parser = argparse.ArgumentParser(description="copy object sample")
# Specify the --region parameter to indicate the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which specifies the name of the destination bucket. This parameter is required.
parser.add_argument('--bucket', help='The name of the destination bucket.', required=True)
# Specify the --endpoint parameter to indicate the endpoint of the region in which the bucket is located. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Specify the --key parameter, which specifies the name of the destination object. This parameter is required.
parser.add_argument('--key', help='The name of the destination object.', required=True)
# Specify the --source_key parameter, which specifies the name of the source object. This parameter is required.
parser.add_argument('--source_key', help='The name of the source object.', required=True)
# Specify the --source_bucket parameter, which specifies the name of the source bucket. This parameter is required.
parser.add_argument('--source_bucket', help='The name of the source bucket.', required=True)
def main():
# Parse the command line parameters.
args = parser.parse_args()
# Obtain access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configurations of the SDK and specify the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Specify the region in which the bucket is located.
cfg.region = args.region
# If the endpoint parameter is provided, specify the endpoint.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the configurations to create an OSSClient instance.
client = oss.Client(cfg)
# Copy objects.
result = client.copy_object(oss.CopyObjectRequest(
bucket=args.bucket, # The name of the destination bucket.
key=args.key, # The key name of the destination object.
source_key=args.source_key, # The key name of the source object.
source_bucket=args.source_bucket, # The name of the source bucket.
))
# Output the result information of the copy operation.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' version id: {result.version_id},'
f' hash crc64: {result.hash_crc64},'
f' source version id: {result.source_version_id},'
f' server side encryption: {result.server_side_encryption},'
f' server side data encryption: {result.server_side_data_encryption},'
f' last modified: {result.last_modified},'
f' etag: {result.etag},'
)
# Call the main function when the script is directly run.
if __name__ == "__main__":
main() # Specify the entry points in the main function of the script when the script is directly run.References
For the complete sample code for copying objects, see copy_object.py.