멀티파트 업로드를 사용한 객체 복사
멀티파트 업로드를 사용한 객체 복사 (Copying an object using multipart upload)
멀티파트 업로드를 사용하면 객체를 여러 부분으로 나눠 복사할 수 있어요. 이 절의 예시는 멀티파트 업로드 API를 사용해 5GB보다 큰 객체를 복사하는 방법을 보여줘요. 멀티파트 업로드에 대한 자세한 내용은 Amazon S3에서 멀티파트 업로드를 사용한 객체 업로드 및 복사를 참고하세요.
출처: 문서
본문
5GB 미만의 객체는 멀티파트 업로드 API를 사용하지 않고 단일 작업으로 복사할 수 있어요. 5GB 미만의 객체는 AWS Management Console, AWS CLI, REST API 또는 AWS SDK를 사용해 복사할 수 있어요. 자세한 내용은 객체 복사, 이동 및 이름 바꾸기를 참고하세요.
추가 체크섬과 함께 멀티파트 업로드로 객체를 업로드하는 종단 간 절차는 멀티파트 업로드로 객체 업로드 및 데이터 무결성 확인 튜토리얼을 참고하세요.
다음 절은 REST API 또는 AWS SDK로 멀티파트 업로드를 사용해 객체를 복사하는 방법을 보여줘요.
Amazon Simple Storage Service API Reference의 다음 절은 멀티파트 업로드의 REST API를 설명해요. 기존 객체를 복사하려면 Upload Part (Copy) API를 사용하고 요청에 x-amz-copy-source 요청 헤더를 추가해 원본 객체를 지정해요.
- Initiate Multipart Upload
- Upload Part
- Upload Part (Copy)
- Complete Multipart Upload
- Abort Multipart Upload
- List Parts
- List Multipart Uploads
이 API를 사용해 직접 REST 요청을 만들거나, 제공하는 SDK 중 하나를 사용할 수 있어요. AWS CLI로 Multipart Upload를 사용하는 방법은 AWS CLI 사용을 참고하세요. SDK에 대한 자세한 내용은 멀티파트 업로드에 대한 AWS SDK 지원을 참고하세요.
로우 레벨 API로 객체를 복사하려면 다음을 수행해요.
AmazonS3Client.initiateMultipartUpload()메서드를 호출해 멀티파트 업로드를 시작해요.AmazonS3Client.initiateMultipartUpload()메서드가 반환하는 응답 객체에서 업로드 ID를 저장해요. 이 업로드 ID를 각 부분 업로드 작업에 제공해요.- 모든 부분을 복사해요. 복사해야 하는 각 부분에 대해
CopyPartRequest클래스의 새 인스턴스를 만들어요. 원본·대상 버킷 이름, 원본·대상 객체 키, 업로드 ID, 부분의 첫 번째와 마지막 바이트 위치, 부분 번호를 포함한 부분 정보를 제공해요. AmazonS3Client.copyPart()메서드 호출의 응답을 저장해요. 각 응답에는 업로드된 부분에 대한ETag값과 부분 번호가 포함돼요. 이 정보는 멀티파트 업로드를 완료할 때 필요해요.AmazonS3Client.completeMultipartUpload()메서드를 호출해 복사 작업을 완료해요.
Java | .NET
AWS SDK for Java로 멀티파트 업로드를 사용해 객체를 복사하는 예시는 Amazon S3 API Reference의 다른 객체에서 객체 부분 복사를 참고하세요.
다음 C# 예시는 SDK for .NET을 사용해 5GB보다 큰 Amazon S3 객체를 한 원본 위치에서 다른 위치(예: 한 버킷에서 다른 버킷으로)로 복사하는 방법을 보여줘요. 5GB보다 작은 객체를 복사하려면 AWS SDK 사용 절에 설명된 단일 작업 복사 절차를 사용해요. Amazon S3 멀티파트 업로드에 대한 자세한 내용은 Amazon S3에서 멀티파트 업로드를 사용한 객체 업로드 및 복사를 참고하세요.
이 예시는 SDK for .NET 멀티파트 업로드 API를 사용해 5GB보다 큰 Amazon S3 객체를 한 S3 버킷에서 다른 S3 버킷으로 복사하는 방법을 보여줘요.
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Amazon.DocSamples.S3
{
class CopyObjectUsingMPUapiTest
{
private const string sourceBucket = "*** provide the name of the bucket with source object ***";
private const string targetBucket = "*** provide the name of the bucket to copy the object to ***";
private const string sourceObjectKey = "*** provide the name of object to copy ***";
private const string targetObjectKey = "*** provide the name of the object copy ***";
// Specify your bucket region (an example region is shown).
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
private static IAmazonS3 s3Client;
public static void Main()
{
s3Client = new AmazonS3Client(bucketRegion);
Console.WriteLine("Copying an object");
MPUCopyObjectAsync().Wait();
}
private static async Task MPUCopyObjectAsync()
{
// Create a list to store the upload part responses.
List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>();
List<CopyPartResponse> copyResponses = new List<CopyPartResponse>();
// Setup information required to initiate the multipart upload.
InitiateMultipartUploadRequest initiateRequest =
new InitiateMultipartUploadRequest
{
BucketName = targetBucket,
Key = targetObjectKey
};
// Initiate the upload.
InitiateMultipartUploadResponse initResponse =
await s3Client.InitiateMultipartUploadAsync(initiateRequest);
// Save the upload ID.
String uploadId = initResponse.UploadId;
try
{
// Get the size of the object.
GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest
{
BucketName = sourceBucket,
Key = sourceObjectKey
};
GetObjectMetadataResponse metadataResponse =
await s3Client.GetObjectMetadataAsync(metadataRequest);
long objectSize = metadataResponse.ContentLength; // Length in bytes.
// Copy the parts.
long partSize = 5 * (long)Math.Pow(2, 20); // Part size is 5 MB.
long bytePosition = 0;
for (int i = 1; bytePosition < objectSize; i++)
{
CopyPartRequest copyRequest = new CopyPartRequest
{
DestinationBucket = targetBucket,
DestinationKey = targetObjectKey,
SourceBucket = sourceBucket,
SourceKey = sourceObjectKey,
UploadId = uploadId,
FirstByte = bytePosition,
LastByte = bytePosition + partSize - 1 >= objectSize ? objectSize - 1 : bytePosition + partSize - 1,
PartNumber = i
};
copyResponses.Add(await s3Client.CopyPartAsync(copyRequest));
bytePosition += partSize;
}
// Set up to complete the copy.
CompleteMultipartUploadRequest completeRequest =
new CompleteMultipartUploadRequest
{
BucketName = targetBucket,
Key = targetObjectKey,
UploadId = initResponse.UploadId
};
completeRequest.AddPartETags(copyResponses);
// Complete the copy.
CompleteMultipartUploadResponse completeUploadResponse =
await s3Client.CompleteMultipartUploadAsync(completeRequest);
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
}
}
}