Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 - Move an object to a different folder

Is there any way to move an object to a different folder in the same bucket using the AWS SDK (preferably for .Net)?

Searching around all I can see is the suggestion of Copy to new location and Delete of the original (Which is easy enough via "CopyObjectRequest" and "DeleteObjectRequest") however I'm just wondering is this the only way?

like image 470
Navik Hiralal Avatar asked Mar 03 '15 11:03

Navik Hiralal


People also ask

Can you move an S3 object?

You can't transfer Amazon S3 bucket ownership between AWS accounts because the bucket is always owned by the account that created it. Instead, you can copy Amazon S3 objects from one bucket to another so that you give ownership of the copied objects to the destination account.

Can S3 object be a folder?

Amazon S3 is a highly-scalable object storage system. Amazon S3 can contain any number of objects (files), and those objects can be organized into “folders”.

What can you use to define actions to move S3 objects between different storage classes?

Lifecycle Management With this action, you can choose to move objects to another storage class. With this, you can configure S3 to move your data between various storage classes on a defined schedule.


2 Answers

Turns out you can use Amazon.S3.IO.S3FileInfo to get the object and then call the "MoveTo" method to move the object.

S3FileInfo currentObject = new S3FileInfo(S3Client,Bucket,CurrentKey);
S3FileInfo movedObject = currentObject.MoveTo(Bucket,NewKey);

EDIT: It turns out the above "MoveTo" method just performs a Copy and Delete behind the scenes anyway :)

For further information:
https://docs.aws.amazon.com/sdkfornet/v3/apidocs/index.html?page=S3/TS3IOS3FileInfo.html&tocid=Amazon_S3_IO_S3FileInfo

like image 129
Navik Hiralal Avatar answered Sep 22 '22 13:09

Navik Hiralal


As @user1527762 mentions S3FileInfo is not available in .NET Core version of the AWSSDK.S3 library (v 3.7.2.2).

If you are in .net core you can use the CopyObject to move an object from one bucket to another.

var s3Client = new AmazonS3Client(RegionEndpoint.USEast1);
var copyRequest = new CopyObjectRequest
                      {
                          SourceBucket = "source-bucket",
                          SourceKey = "fileName",
                          DestinationBucket = "dest-bucket",
                          DestinationKey = "fileName.jpg",
                          CannedACL = S3CannedACL.PublicRead,
                       };
 var copyResponse = await s3Client.CopyObjectAsync(copyRequest);

CopyObject does not delete the source object so you would have to call delete like this:

 DeleteObjectRequest request = new DeleteObjectRequest
            {
                BucketName = "source-bucket",
                Key = "fileName.jpg"
            };

            await s3Client .DeleteObjectAsync(request);

Details on CopyObject here: https://docs.aws.amazon.com/AmazonS3/latest/userguide/copy-object.html

And DeleteObject here: https://docs.aws.amazon.com/AmazonS3/latest/userguide/delete-objects.html

like image 20
Chris Morgan Avatar answered Sep 18 '22 13:09

Chris Morgan