Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy/move object of amazon s3 having multi level childs to destination?

I have to implement copy/move functionality of amazon s3 objects. but in case of objects with multilevel childs i am unable to copy/move all objects.

like image 786
murtaza sanjeliwala Avatar asked May 10 '16 09:05

murtaza sanjeliwala


2 Answers

you can use S3DirectoryInfo.Move and S3DirectoryInfo.Copy methods see the examples below (AWSSDK.Core and AWSSDK.S3 Version 3.1.0.0):enter code here

AmazonS3Config cfg = new AmazonS3Config();
cfg.RegionEndpoint = Amazon.RegionEndpoint.EUCentral1;//bucket location
string bucketName = "source bucket";
AmazonS3Client s3Client = new AmazonS3Client("your accessKey", "your secretKey", cfg);

S3DirectoryInfo source = new S3DirectoryInfo(s3Client, bucketName, "sourceFolder");
 string bucketName2 = "destination butcket";               
    S3DirectoryInfo destination = new S3DirectoryInfo(s3Client, bucketName2);
  source.CopyTo(destination); 
// or 
source.MoveTo(destination);

Probably it can help you.

like image 164
Luis Fernando Camacho Camacho Avatar answered Oct 05 '22 09:10

Luis Fernando Camacho Camacho


If you are doing this in .Net Core, you won't have access to the DirectoryInfoClass (referenced in the answers above), as the Amazon.S3.IO namespace is not included in the AWS S3 SDK for .net core.

Instead copy and delete the objects as illustrated below:

    public async Task MoveObjectAsync(string srcBucket, string srcKey, string destBucket, string destKey)
    {
        CopyObjectRequest request = new CopyObjectRequest
        {
            SourceBucket = srcBucket,
            SourceKey = srcKey,
            DestinationBucket = destBucket,
            DestinationKey = destKey                
        };
        CopyObjectResponse response = await _client.CopyObjectAsync(request);

        var request = new DeleteObjectRequest
        {
            BucketName = bucket,
            Key = srcKey
        };
        var response = await _client.DeleteObjectAsync(request);
    }
like image 41
Alan Wolman Avatar answered Oct 05 '22 10:10

Alan Wolman