如何使用AWS api将一个目录的内容复制到另一个目录?

时间:2015-09-15 02:01:33

标签: c# amazon-s3

  • bucketName / folder1中/ image.jpg的
  • bucketName / folder1中/ someDoc.doc
  • bucketName / folder1中/ somePdf.pdf
  • bucketName /文件夹2 / someOtherImage.jpeg

我希望能够将folder1的所有内容复制到folder2,并将其从文件夹1中删除。

2 个答案:

答案 0 :(得分:0)

我认为您可以使用AWSSDK(3.x.x版本)C#来使用S3DirectoryInfo类。 这个类有MoveTo方法,请看下面的代码。

        public static void MoveFiles()
        {
            BasicAWSCredentials basicCredentials = new BasicAWSCredentials("access key", "secret key");
            AmazonS3Config configurationClient = new AmazonS3Config();
            configurationClient.RegionEndpoint = RegionEndpoint.EUCentral1;//region of bucket
            try
            {
                using (AmazonS3Client clientConnection = new AmazonS3Client(basicCredentials, configurationClient))
                {
                    S3DirectoryInfo source = new S3DirectoryInfo(clientConnection, "sourcebucketname", "sourcefolderkey");
                    S3DirectoryInfo target = new S3DirectoryInfo(clientConnection, "destinationbucketname", "destinationfolderkey");
                    source.MoveTo(target);// move all  content from FolderNameUniTest109 to FolderNameUniTest179

                }
            }
            catch(Exception ex)
            {

            }



        }

答案 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);

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