小さいサイズのオブジェクトをコピーする

小さいサイズのオブジェクトをコピーする完全なコードについては、『GitHub』をご参照ください。

小さいサイズのオブジェクトをコピーする前に、次の制限事項に注意してください。

  • ソースオブジェクトに対する読み取りおよび書き込み権限が必要です。
  • あるリージョンから別のリージョンにオブジェクトをコピーすることはできません。 たとえば、中国 (杭州) リージョンのバケットから中国 (青島) リージョンのバケットにオブジェクトをコピーすることはできません。
  • オブジェクトサイズは最大 1 GB に制限されています。

小さいサイズのオブジェクトをコピーするには、次のコードを実行します。

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var sourceBucket = "<yourSourceBucketName>";
var sourceObject = "<yourSourceObjectName>";
var targetBucket = "<yourDestBucketName>";
var targetObject = "<yourDestObjectName>";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    var metadata = new ObjectMetadata();
    metadata.AddHeader("mk1", "mv1");
    metadata.AddHeader("mk2", "mv2");
    var req = new CopyObjectRequest(sourceBucket, sourceObject, targetBucket, targetObject)
    {
        // If the value of NewObjectMetadata is null, the COPY mode is used (the metadata of the source object is copied). If the value of NewObjectMetadata is not null, the REPLACE mode is used (the metadata of the source object is replaced).
        NewObjectMetadata = metadata 
    };
    // Copy an object.
    client.CopyObject(req);
    Console.WriteLine("Copy object succeeded");
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID: {2} \tHostID: {3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
} catch (Exception e) {
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

大きいサイズのオブジェクトをコピー

大きいサイズのオブジェクトをコピーするための完全なコードについては、『GitHub』をご参照ください。

  • UploadPartCopy

    1 GB を超えるオブジェクトをコピーするには、UploadPartCopy を使用する必要があります。

    1. UploadPartCopy イベントを開始するには、InitiateMultipartUploadRequest を使用します。
    2. UploadPartCopy メソッドを使用してマルチパートコピーを実行します。
    3. CompleteMultipartUpload メソッドを使用してオブジェクトのコピーを完了します。

    UploadPartCopy タスクに対して次のコードを実行します。

    using Aliyun.OSS;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var sourceBucket = "<yourSourceBucketName>";
    var sourceObject = "<yourSourceObjectName>";
    var targetBucket = "<yourDestBucketName>";
    var targetObject = "<yourDestObjectName>";
    var uploadId = "";
    var partSize = 50 * 1024 * 1024;
    // Create an OSSClient instance.
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    try
    {
        // Initiate a copy task. You can use the InitiateMultipartUploadRequest method to specify the metadata information about the target object.
        var request = new InitiateMultipartUploadRequest(targetBucket, targetObject);
        var result = client.InitiateMultipartUpload(request);
        // Print the uploadId.
        uploadId = result.UploadId;
        Console.WriteLine("Init multipart upload succeeded, Upload Id: {0}", result.UploadId);
        // Calculate the number of parts.
        var metadata = client.GetObjectMetadata(sourceBucket, sourceObject);
        var fileSize = metadata.ContentLength;
        var partCount = (int)fileSize / partSize;
        if (fileSize % partSize ! = 0)
        {
            partCount++;
        }
        // Start the UploadPartCopy task.
        var partETags = new List<PartETag>();
        for (var i = 0; i < partCount; i++)
        {
            var skipBytes = (long)partSize * i;
            var size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
            // Create UploadPartCopyRequest. You can specify conditions with UploadPartCopyRequest.
            var uploadPartCopyRequest = new UploadPartCopyRequest(targetBucket, targetObject, sourceBucket, sourceObject, uploadId)
                {
                    PartSize = size,
                    PartNumber = i + 1,
                    // Beginindex is used to locate the location that the last UploadPartCopy starts.
                    BeginIndex = skipBytes
                };
            // Call the uploadPartCopy method to copy each part.
            var uploadPartCopyResult = client.UploadPartCopy(uploadPartCopyRequest);
            Console.WriteLine("UploadPartCopy : {0}", i);
            partETags.Add(uploadPartCopyResult.PartETag);
        }
        // Complete the UploadPartCopy task.
        var completeMultipartUploadRequest =
        new CompleteMultipartUploadRequest(targetBucket, targetObject, uploadId);
        // The partETags refers to the list of partETags saved during the multipart upload. After OSS receives the part list submitted by the user, it verifies the validity of each data part one by one. After the verification is passed, OSS conbine these parts into a complete object.
        foreach (var partETag in partETags)
        {
            completeMultipartUploadRequest.PartETags.Add(partETag);
        }
        var completeMultipartUploadResult = client.CompleteMultipartUpload(completeMultipartUploadRequest);
        Console.WriteLine("CompleteMultipartUpload succeeded");
    }
    catch (OssException ex)
    {
        Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID: {2} \tHostID: {3}",
            ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
    }
    } catch (Exception e) {
    {
        Console.WriteLine("Failed with error info: {0}", ex.Message);
    }
  • 再開可能コピー

    コピーが中断された場合は、再開可能コピーを使用してコピーを続行できます。 再開可能コピーの完全なコードについては、『GitHub』をご参照ください。

    再開可能コピーには、次のコードを実行します。

    using Aliyun.OSS;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var sourceBucket = "<yourSourceBucketName>";
    var sourceObject = "<yourSourceObjectName>";
    var targetBucket = "<yourDestBucketName>";
    var targetObject = "<yourDestObjectName>";
    var checkpointDir = @"<yourCheckpointDir>";
    // Create an OSSClient instance.
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    try
    {
        var request = new CopyObjectRequest(sourceBucket, sourceObject, targetBucket, targetObject);
        // The checkpointDir directory stores the intermediate state information of a resumable upload. The information will be used when a failed upload task is resumed. // If the checkpointDir directory is null, resumable copy will not take effect, and the object that previously failed to be copied will be copied all over again.
        client.ResumableCopyObject(request, checkpointDir);
        Console.WriteLine("Resumable copy new object:{0} succeeded", request.DestinationKey);
    }
    } catch (Exception e) {
    {
        Console.WriteLine("Resumable copy new object failed, {0}", ex.Message);
    }