This topic describes how to use the CopyObjectAsync method in the C# SDK V2 to copy objects (smaller than 5 GiB) from a source bucket to a destination bucket within the same region.
Considerations
The sample code in this topic uses the China (Hangzhou) region ID (
cn-hangzhou) and the public endpoint by default. If you want to access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about OSS regions and endpoints, see OSS regions and endpoints.To copy an object, you must have read permissions on the source object and read and write permissions on the destination bucket.
The source bucket and destination bucket must be in the same region. For example, objects in a bucket located in the China (Hangzhou) region cannot be copied to a bucket located in the China (Qingdao) region.
When you copy objects, you must ensure that neither the source bucket nor the destination bucket has a retention policy. Otherwise, the The object you specified is immutable. error 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 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. | |
|
Sample code
You can use the following code to copy an object smaller than 5 GiB from the source bucket to the destination bucket.
using OSS = AlibabaCloud.OSS.V2; // Create an alias for Alibaba Cloud OSS SDK to simplify subsequent use
var region = "cn-hangzhou"; // Required. Specify the region of the bucket. In this example, the region is China (Hangzhou), and the region ID is cn-hangzhou
var endpoint = null as string; // Optional. Specify the endpoint used to access OSS. In this example, the endpoint for China (Hangzhou) is https://oss-cn-hangzhou.aliyuncs.com
var srcBucket = "your srcBucket name"; // Required. Specify the name of the source bucket
var srcKey = "your srcKey name"; // Required. Specify the name of the object in the source bucket in the folder/objectName format
var dstBucket = "your dstBucket name"; // Required. Specify the name of the destination bucket
var dstKey = "your dstkey name"; // Required. Specify the name of the object in the destination bucket in the folder/objectName format
// Load the default configuration of the OSS SDK, which automatically reads credential information (such as AccessKey) from environment variables
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set to use environment variables for credentials authentication (format: OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET)
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the bucket region in the configuration
cfg.Region = region;
// If an endpoint is specified, override the default endpoint
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance using the configuration information
using var client = new OSS.Client(cfg);
// Call the CopyObjectAsync method to perform the OSS object copy operation
var result = await client.CopyObjectAsync(new OSS.Models.CopyObjectRequest()
{
Bucket = dstBucket,
Key = dstKey,
SourceBucket = srcBucket,
SourceKey = srcKey
});
// Display the upload result
Console.WriteLine("CopyObject done"); // Indicate that the operation is complete
Console.WriteLine($"StatusCode: {result.StatusCode}"); // HTTP status code
Console.WriteLine($"RequestId: {result.RequestId}"); // RequestId, used for troubleshooting by Alibaba Cloud
Console.WriteLine("Response Headers:"); // Response header information
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headersReferences
For the complete sample code for copying objects, see CopyObject.cs.