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?
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.
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”.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With