このトピックでは、バケット内または同じリージョン内のバケット間でオブジェクトをコピーする方法について説明します。
使用上の注意
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。
オブジェクトをコピーするには、ソースオブジェクトに対する読み取り権限と、宛先バケットに対する読み取りおよび書き込み権限が必要です。
ソースバケットとターゲットバケットに保持ポリシーが設定されていないことを確認します。 それ以外の場合、エラーメッセージ指定したオブジェクトは不変です。 が返されます。
ソースバケットと宛先バケットは同じリージョンにある必要があります。 たとえば、中国 (杭州) リージョンにあるバケットから中国 (青島) リージョンにある別のバケットにオブジェクトをコピーすることはできません。
小さなオブジェクトをコピーする
次のコードは、小さなオブジェクトをコピーする方法の例を示しています。
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the source bucket. Example: srcexamplebucket.
var sourceBucket = "srcexamplebucket";
// Specify the full path of the source object. Do not include the bucket name in the full path. Example: srcdir/scrobject.txt.
var sourceObject = "srcdir/scrobject.txt";
// Specify the name of the destination bucket. The destination bucket must be in the same region as the source bucket. Example: destbucket.
var targetBucket = "destbucket";
// Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destdir/destobject.txt.
var targetObject = "destdir/destobject.txt";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
c.SetRegion(region);
try
{
var metadata = new ObjectMetadata();
// Specify user metadata. The user metadata is stored as key-value pairs. For example, the key is mk1 and the value is mv1.
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 and the metadata of the source object is copied to the destination object. If the value of NewObjectMetadata is not null, the REPLACE mode is used and the metadata of the destination object is overwritten by the metadata specified in the request.
NewObjectMetadata = metadata
};
// Copy the 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 ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
大きなオブジェクトをコピーする
マルチパートコピーを使用したオブジェクトのコピー
サイズが1 GBを超えるオブジェクトをコピーするには、オブジェクトをパーツに分割し、UploadPartCopyを使用してパーツを順番にコピーする必要があります。 マルチパートコピーを実装するには、次の手順を実行します。
InitiateMultipartUploadRequestメソッドを呼び出して、マルチパートコピータスクを開始します。
マルチパートコピーを実行するには、UploadPartCopyメソッドを呼び出します。
CompleteMultipartUploadメソッドを呼び出して、マルチパートコピータスクを完了します。
次のコードは、マルチパートコピーを実行する方法の例を示しています。
using Aliyun.OSS; using Aliyun.OSS.Common; // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. var endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // 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. var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID"); var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET"); // Specify the name of the source bucket. Example: srcexamplebucket. var sourceBucket = "srcexamplebucket"; // Specify the full path of the source object. Do not include the bucket name in the full path. Example: srcdir/scrobject.txt. var sourceObject = "srcdir/scrobject.txt"; // Specify the name of the destination bucket. The destination bucket must be in the same region as the source bucket. Example: destbucket. var targetBucket = "destbucket"; // Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destdir/destobject.txt. var targetObject = "destdir/destobject.txt"; var uploadId = ""; var partSize = 50 * 1024 * 1024; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. const string region = "cn-hangzhou"; // Create a ClientConfiguration instance and modify the default parameters based on your requirements. var conf = new ClientConfiguration(); // Use the signature algorithm V4. conf.SignatureVersion = SignatureVersion.V4; // Create an OSSClient instance. var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf); c.SetRegion(region); try { // Initiate a multipart copy task. You can call the InitiateMultipartUploadRequest operation to specify the metadata of the destination object. var request = new InitiateMultipartUploadRequest(targetBucket, targetObject); var result = client.InitiateMultipartUpload(request); // Display the upload ID. uploadId = result.UploadId; Console.WriteLine("Init multipart upload succeeded, Upload Id: {0}", result.UploadId); // Calculate the total 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 multipart copy 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 an UploadPartCopyRequest object. You can call UploadPartCopyRequest to specify conditions. var uploadPartCopyRequest = new UploadPartCopyRequest(targetBucket, targetObject, sourceBucket, sourceObject, uploadId) { PartSize = size, PartNumber = i + 1, // Use BeginIndex to find the start position to copy the part. 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 multipart copy task. var completeMultipartUploadRequest = new CompleteMultipartUploadRequest(targetBucket, targetObject, uploadId); // partETags is a list of partETags. OSS verifies the validity of each part after it receives partETags. After all parts are verified, OSS combines 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 ex) { Console.WriteLine("Failed with error info: {0}", ex.Message); }
再開可能コピー
再開可能なコピータスクが中断された場合は、コピータスクを続行できます。
次のコードは、再開可能コピーを使用してオブジェクトをコピーする方法の例を示しています。
using Aliyun.OSS; // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. var endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // 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. var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID"); var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET"); // Specify the name of the source bucket. Example: srcexamplebucket. var sourceBucket = "srcexamplebucket"; // Specify the full path of the source object. Do not include the bucket name in the full path. Example: srcdir/scrobject.txt. var sourceObject = "srcdir/scrobject.txt"; // Specify the name of the destination bucket. The destination bucket must be in the same region as the source bucket. Example: destbucket. var targetBucket = "destbucket"; // Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destdir/destobject.txt. var targetObject = "destdir/destobject.txt"; // Specify the checkpoint file that records the results of the resumable copy task. This file stores information about the copy progress. If an object fails to be copied, the next copy operation continues based on the recorded progress. After the object is copied, the checkpoint file is deleted. var checkpointDir = @"yourCheckpointDir"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. const string region = "cn-hangzhou"; // Create a ClientConfiguration instance and modify the default parameters based on your requirements. var conf = new ClientConfiguration(); // Use the signature algorithm V4. conf.SignatureVersion = SignatureVersion.V4; // Create an OSSClient instance. var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf); c.SetRegion(region); try { var request = new CopyObjectRequest(sourceBucket, sourceObject, targetBucket, targetObject); // Specify the checkpointDir directory in which the checkpoint file is stored. The checkpoint file records the state of the resumable copy task and can be used to resume the task in case of failures. If you set checkpointDir to null, resumable copy does not take effect, and the object must be copied again from the start if it fails to be copied. client.ResumableCopyObject(request, checkpointDir); Console.WriteLine("Resumable copy new object:{0} succeeded", request.DestinationKey); } catch (Exception ex) { Console.WriteLine("Resumable copy new object failed, {0}", ex.Message); }
関連ドキュメント
小さなオブジェクトをコピーする
小さなオブジェクトのコピーに使用される完全なサンプルコードについては、GitHubをご覧ください。
小さなオブジェクトをコピーするために呼び出すことができるAPI操作の詳細については、「CopyObject」をご参照ください。
大きなオブジェクトをコピーする
ラージオブジェクトのコピーに使用される完全なサンプルコードについては、GitHubをご覧ください。
ラージオブジェクトをコピーするために呼び出すことができるAPI操作の詳細については、「UploadPartCopy」をご参照ください。
再開可能コピー
再開可能コピーの実行に使用される完全なサンプルコードについては、『GitHub』をご参照ください。