Copy an object (C# SDK V2)

Updated at:

This topic describes how to copy an object in a versioning-enabled bucket. You can call the CopyObject operation to copy an object that is smaller than 1 GB. To copy an object that is larger than 1 GB, use multipart copy (UploadPartCopy).

Notes

  • The sample code in this topic uses the China (Hangzhou) region, whose region ID is cn-hangzhou, as an example. By default, the public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints.

  • To copy an object, you must have the oss:GetObject and oss:PutObject permissions. For more information, see Grant custom permissions to a RAM user.

Sample code

Copy an object

Note

For an object smaller than 1 GB, you can use the CopyObject method to copy the object from a source bucket to a destination bucket in the same region.

  • By default, x-oss-copy-source is used to copy the current version of an object. If the current version is a delete marker, OSS returns a 404 Not Found error. You can add a versionId to x-oss-copy-source to copy a specific version of the object. Delete markers cannot be copied.

  • You can copy an earlier version of an object to the same bucket. The copied version becomes the new current version. This lets you restore an earlier version of the object.

  • If versioning is enabled for the destination bucket, OSS automatically generates a unique version ID for the copied object. The version ID is returned in the x-oss-version-id header of the response. If versioning is not enabled or is suspended for the destination bucket, OSS automatically generates a version of the copied object with a null version ID. This version overwrites the original version that also has a null version ID.

  • If versioning is enabled or suspended for the destination bucket, you cannot copy an appendable object.

You can use the following sample code to copy an object.

using OSS = AlibabaCloud.OSS.V2;  // Create an alias for Alibaba Cloud OSS SDK to simplify subsequent use.

var region = "cn-hangzhou";  // Required. The region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to cn-hangzhou.
var endpoint = null as string;  // Optional. The endpoint that is used to access OSS. For example, if the bucket is in the China (Hangzhou) region, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var srcBucket = "your srcBucket name";  // Required. The name of the source bucket.
var srcKey = "your srcKey name";  // Required. The name of the object in the source bucket. Format: folder/objectName.
var versionId = "your srcKey versionId";  // Required. The x-oss-version-id of the source object.
var dstBucket = "your dstBucket name";  // Required. The name of the destination bucket.
var dstKey = "your dstkey name";  // Required. The name of the object in the destination bucket. Format: folder/objectName.

// Load the default configurations of the OSS SDK. The configurations automatically read credential information (such as AccessKey) from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set the use of environment variables to obtain credentials for identity verification. Format: OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket.
cfg.Region = region;   
// If an endpoint is specified, it overwrites 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 copy an OSS object.
var result = await client.CopyObjectAsync(new OSS.Models.CopyObjectRequest()
{
    Bucket = dstBucket,
    Key = dstKey,
    SourceBucket = srcBucket,
    SourceKey = srcKey,
    SourceVersionId = versionId
});

// Print the upload result.
Console.WriteLine("CopyObject done");  // A message indicating that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}");  // The HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}");  // The request ID, which is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:");  // The response header information.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));  // Traverse and print all response headers.