如何将具有多级孩子的亚马逊s3的对象复制/移动到目的地?

时间:2016-05-10 09:28:28

标签: c# asp.net-mvc amazon-s3

我必须实现amazon s3对象的复制/移动功能。但是对于具有多级子项的对象,我无法复制/移动所有对象。

3 个答案:

答案 0 :(得分:4)

您可以使用S3DirectoryInfo.Move和S3DirectoryInfo.Copy方法查看以下示例(AWSSDK.Core和AWSSDK.S3版本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);

可能它可以帮助你。

答案 1 :(得分:0)

试试这个:

            PutObjectRequest request = new PutObjectRequest();
            request.WithBucketName(BUCKET_NAME);
            request.WithKey(PrefixFolder + folderPath[i] + addProjectResponse.ProjectName.Replace("_", " ") + "/");
            request.WithTimeout(-1);
            request.WithReadWriteTimeout(60 * 60 * 1000);
            request.WithContentBody("");
            s3Client.PutObject(request);
            S3DirectoryInfo source = new S3DirectoryInfo(s3Client, BUCKET_NAME, PrefixFolder + folderPath[i] + addProjectResponse.OldProjectName.Replace("_", " "));
            S3DirectoryInfo destination = new S3DirectoryInfo(s3Client, BUCKET_NAME, PrefixFolder + folderPath[i] + addProjectResponse.ProjectName.Replace("_", " "));
            source.CopyTo(destination);

            source.Delete(true);

这里,一旦你从一个文件夹复制到另一个文件夹,第一个文件夹就会被删除。

答案 2 :(得分:0)

如果您在.Net Core中执行此操作,则您将无权访问DirectoryInfoClass(在以上答案中引用),因为Amazon.S3.IO名称空间未包含在.net Core的AWS S3 SDK中

相反,如下图所示复制和删除对象:

    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 = key
        };
        var response = await _client.DeleteObjectAsync(request);
    }