This topic describes how to use the CopyObject method of the Swift SDK to copy an object (smaller than 5 GiB) from a source bucket to a destination bucket in the same region.
Notes
The sample code in this topic uses the China (Hangzhou) region ID,
cn-hangzhou, as an example. A public Endpoint is used by default. To access OSS from other Alibaba Cloud products in the same region, you can use an internal Endpoint. For more information about the regions and Endpoints that OSS supports, see Regions and Endpoints.To copy a file, you must have read permissions on the source file and read and write permissions on the destination bucket.
Cross-region copy is not supported. For example, you cannot copy a file from a bucket in the China (Hangzhou) region to a bucket in the China (Qingdao) region.
When you copy a file, ensure that neither the source bucket nor the destination bucket has a retention policy configured. Otherwise, the error message The object you specified is immutable. is returned.
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 policies.
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. | |
|
Sample code
You can use the following code to copy an object smaller than 5 GiB from a source bucket to a destination bucket.
import AlibabaCloudOSS
import Foundation
@main
struct Main {
static func main() async {
do {
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to cn-hangzhou.
let region = "cn-hangzhou"
// Specify the bucket name.
let bucket = "yourBucketName"
// Optional. Specify the domain name used to access OSS. For example, if the bucket is in the China (Hangzhou) region, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
let endpoint: String? = nil
// The name of the destination object, for example, copied-object.txt.
let key = "copied-object.txt"
// The name of the source bucket. Replace the value with the actual bucket name.
let sourceBucket = "source-bucket-name"
// The name of the source object, for example, original.txt.
let sourceKey = "original-object.txt"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
let credentialsProvider = EnvironmentCredentialsProvider()
// Configure the parameters for the OSS client.
let config = Configuration.default()
.withRegion(region) // Set the region.
.withCredentialsProvider(credentialsProvider) // Set the credentials.
// Set the Endpoint.
if let endpoint = endpoint {
config.withEndpoint(endpoint)
}
// Create an OSS client instance.
let client = Client(config)
// Copy the source object from the source bucket to the destination path in the destination bucket.
let result = try await client.copyObject(
CopyObjectRequest(
bucket: bucket,
key: key,
sourceBucket: sourceBucket,
sourceKey: sourceKey
)
)
print("result:\n\(result)") // Print the operation result.
} catch {
// Stop the program and print the error message.
print("error: \(error)")
}
}
}
References
For the complete sample code for copying an object, see GitHub example.
For more information about the API operation for copying an object, see CopyObject.