All Products
Search
Document Center

Object Storage Service:Copy an object (Ruby SDK)

Last Updated:Mar 20, 2026

Copy an object from a source bucket to the same or a different destination bucket in the same region using bucket.copy_object.

Prerequisites

Before you begin, make sure that you have:

  • Read permissions on the source object

  • Read and write permissions on the destination bucket

  • The source and destination buckets in the same region

Usage notes

  • Endpoint: The examples use the public endpoint for the China (Hangzhou) region (https://oss-cn-hangzhou.aliyuncs.com). To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For supported regions and endpoints, see Regions and endpoints.

  • Access credentials: The examples load credentials from environment variables. For configuration details, see Configure access credentials.

  • OSSClient initialization: The examples create an OSSClient instance using an OSS endpoint. To initialize with a custom domain name or Security Token Service (STS), see Configuration examples for common scenarios.

  • Same-region constraint: Objects can only be copied between buckets in the same region. For example, an object in the China (Hangzhou) region cannot be copied to a bucket in the China (Qingdao) region.

  • Retention policies: If a retention policy is configured on either the source or destination bucket, the copy operation fails with: The object you specified is immutable.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default and must be granted access via RAM Policy or bucket policy.

Required permissions:

ActionCondition
oss:GetObjectAlways required (read source object)
oss:PutObjectAlways required (write destination object)
oss:GetObjectVersionOnly when specifying the source object version via versionId
oss:GetObjectTaggingOnly when copying object tags via x-oss-tagging
oss:PutObjectTaggingOnly when copying object tags via x-oss-tagging
oss:GetObjectVersionTaggingOnly when copying tags of a specific version via versionId
kms:GenerateDataKeyOnly when the destination metadata includes X-Oss-Server-Side-Encryption: KMS
kms:DecryptOnly when the destination metadata includes X-Oss-Server-Side-Encryption: KMS

Copy an object

Use :meta_directive to control how object metadata is handled during the copy:

MetaDirective valueBehavior
Aliyun::OSS::MetaDirective::COPYThe destination object inherits the source object's metadata
Aliyun::OSS::MetaDirective::REPLACEThe source object's metadata is replaced with the new metadata you specify

If you omit :meta_directive, the destination object inherits the source object's metadata (equivalent to COPY).

require 'aliyun/oss'

# Initialize the OSS client.
# @param endpoint [String] The OSS endpoint for the bucket's region.
#   Format: https://oss-{region-id}.aliyuncs.com
# @param access_key_id [String] AccessKey ID from the OSS_ACCESS_KEY_ID environment variable.
# @param access_key_secret [String] AccessKey secret from the OSS_ACCESS_KEY_SECRET environment variable.
client = Aliyun::OSS::Client.new(
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

# Get a Bucket instance for the bucket that contains the source object.
# @param bucket_name [String] The name of the source bucket.
# @return [Aliyun::OSS::Bucket] A Bucket instance for subsequent operations.
bucket = client.get_bucket('examplebucket')

# Copy srcobject.txt to destobject.txt, preserving the source object's metadata.
bucket.copy_object(
  'destobject.txt', 'srcobject.txt',
  :meta_directive => Aliyun::OSS::MetaDirective::COPY
)

# Copy srcobject.txt to destobject.txt and replace its metadata with new values.
bucket.copy_object(
  'destobject.txt', 'srcobject.txt',
  :metas => { 'year' => '2017' },
  :meta_directive => Aliyun::OSS::MetaDirective::REPLACE
)

Replace the placeholder values before running the code:

PlaceholderDescriptionExample
examplebucketName of the bucket containing the source objectmy-bucket
srcobject.txtKey of the source objectimages/photo.jpg
destobject.txtKey of the destination objectimages/photo-copy.jpg

What's next