Copy objects (Swift SDK)
Use the CopyObject API to copy an object smaller than 5 GiB between buckets in the same region — without downloading and re-uploading it.
With CopyObject, you can:
Copy an object to another bucket in the same region
Rename an object by copying it to a new key and deleting the original
Note: To copy objects larger than 5 GiB, use multipart copy. See Multipart copy (Swift SDK).
Prerequisites
Before you begin, ensure that you have:
Read permissions on the source object
Read and write permissions on the destination bucket
Both the source and destination buckets in the same region
Usage notes
Cross-region copy is not supported. Both buckets must be in the same region. For example, you cannot copy from China (Hangzhou) to China (Qingdao).
Retention policies block copy operations. If either the source or destination bucket has a retention policy configured, the copy fails with:
The object you specified is immutable.Endpoint configuration: The sample code uses the China (Hangzhou) region (
cn-hangzhou) with a public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. See Regions and endpoints.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default — the account owner or administrator must grant the required actions through a RAM Policy or Bucket policies.
| API | Required action | When it applies |
|---|---|---|
| CopyObject | oss:GetObject | Always required |
| CopyObject | oss:PutObject | Always required |
| CopyObject | oss:GetObjectVersion | Required when specifying the source object version via versionId |
| CopyObject | oss:GetObjectTagging, oss:PutObjectTagging | Required when copying object tags via x-oss-tagging |
| CopyObject | oss:GetObjectVersionTagging | Required when specifying tags of a specific object version via versionId |
| CopyObject | kms:GenerateDataKey, kms:Decrypt | Required when the destination object uses X-Oss-Server-Side-Encryption: KMS |
Copy an object
The following example copies original-object.txt from source-bucket-name to the destination bucket as copied-object.txt.
Credentials are read from environment variables. Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running the code.
import AlibabaCloudOSS
import Foundation
@main
struct Main {
static func main() async {
do {
// Region where the bucket is located, for example, cn-hangzhou for China (Hangzhou).
let region = "cn-hangzhou"
// Destination bucket name.
let bucket = "yourBucketName"
// Optional: set a custom endpoint, or leave nil to use the default public endpoint.
let endpoint: String? = nil
// Destination object key.
let key = "copied-object.txt"
// Source bucket name.
let sourceBucket = "source-bucket-name"
// Source object key.
let sourceKey = "original-object.txt"
// Read credentials from environment variables.
// Avoid hardcoding access keys in source code.
let credentialsProvider = EnvironmentCredentialsProvider()
// Configure the OSS client.
let config = Configuration.default()
.withRegion(region)
.withCredentialsProvider(credentialsProvider)
if let endpoint = endpoint {
config.withEndpoint(endpoint)
}
let client = Client(config)
// Copy the source object to the destination bucket.
let result = try await client.copyObject(
CopyObjectRequest(
bucket: bucket,
key: key,
sourceBucket: sourceBucket,
sourceKey: sourceKey
)
)
print("Copy result: \(result)")
} catch {
print("Error: \(error)")
}
}
}What's next
Full sample code: GitHub — CopyObject example
API reference: CopyObject