Yoope
Yoope

Reputation: 2195

AWS S3 How to move files from a bucket to another bucket in a different region

I have two buckets in different regions, B1 is in APSoutheast2 (Sydney) and the other (B2) is in USWest1 (US West (N. California)). I'm trying to move some files from B1 to B2 using AWS S3 SDK (.NET) but I don't seem to see an easy way of doing it.

Here's my sample code:

var sydneyClient = new AmazonS3Client("accessKeyID", "secretKey", 
RegionEndpoint.APSoutheast2)
var fileInfo = new S3FileInfo(sydneyClient, "b1", "filePath");
if (!fi.Exists)
{
    return;
}
fileInfo.MoveTo("b2", "filePath");

This gave me the following error:

Amazon.S3.AmazonS3Exception: 'Error making request with Error Code 
MovedPermanently and Http Status Code MovedPermanently. No further error 
information was returned by the service.'

I think it's because b2 is in a different region as when I do this:

var listAllRequest = new ListObjectsRequest();
listAllRequest.BucketName = "b2";
var listAllResponse = sydneyClient.ListObjects(listAllRequest);

I got this:

The bucket you are attempting to access must be addressed using the specified 
endpoint. Please send all future requests to this endpoint

I know it makes sense that my "sydneyClient" cannot access another bucket created in the US as I specify the region as "RegionEndpoint.APSoutheast2" when creating it and each region is independent according to AWS doc.

If I try moving the file from b1 to another bucket in APSoutheast2, it then works fine.

I know I can download the file from b1 to local storage, create another AmazonS3Client for b2 and then do the upload. But is there an easy way to move a file from b1 to a bucket in another region in this case?

Thanks

Upvotes: 4

Views: 5747

Answers (2)

Gapton
Gapton

Reputation: 2124

You can configure using AWS Management Portal directly without having to change any code.

This feature is called S3 Cross Region Replication (CRR for short).

They have a setup guide here.

Upvotes: 0

John Rotenstein
John Rotenstein

Reputation: 270154

Amazon S3 does not have a "Move" command. Instead, you should use the AmazonS3Client.CopyObject Method, specifying:

public virtual CopyObjectResponse CopyObject(
         String sourceBucket,
         String sourceKey,
         String destinationBucket,
         String destinationKey
)

Then, delete the original object.

This works between buckets and also between regions. Send the request to the destination region (that is, to the region where the destination bucket exists).

Upvotes: 5

Related Questions