【问题标题】:Coping folder inside AmazonS3 Bucket (c#)在 Amazon S3 存储桶中复制文件夹(c#)
【发布时间】:2016-05-04 13:12:30
【问题描述】:

我想将一个包含所有现有文件的文件夹复制到 AmazonS3 的同一个存储桶中的另一个文件夹。

我可以复制一个对象,但我需要将包含所有文件的文件夹复制到另一个文件夹中。

【问题讨论】:

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


    【解决方案1】:

    这是在 AmazonS3 Bucket 中复制文件夹的示例,这对我有用。 更多详情可以查看link

      public bool CopyFolderInsideS3Bucket(string source, string destination)
            {
                var strippedSource = source;
                var strippedDestination = destination;
    
                // process source
                if (strippedSource.StartsWith("/"))
                    strippedSource = strippedSource.Substring(1);
                if (strippedSource.EndsWith("/"))
                    strippedSource = source.Substring(0, strippedSource.Length - 1);
    
                var sourceParts = strippedSource.Split('/');
                var sourceBucket = sourceParts[0];
    
                var sourcePrefix = new StringBuilder();
                for (var i = 1; i < sourceParts.Length; i++)
                {
                    sourcePrefix.Append(sourceParts[i]);
                    sourcePrefix.Append("/");
                }
    
                // process destination
                if (strippedDestination.StartsWith("/"))
                    strippedDestination = destination.Substring(1);
                if (strippedDestination.EndsWith("/"))
                    strippedDestination = destination.Substring(0, strippedDestination.Length - 1);
    
                var destinationParts = strippedDestination.Split('/');
                var destinationBucket = destinationParts[0];
    
                var destinationPrefix = new StringBuilder();
                for (var i = 1; i < destinationParts.Length; i++)
                {
                    destinationPrefix.Append(destinationParts[i]);
                    destinationPrefix.Append("/");
                }
    
                var listObjectsResult = client.ListObjects(new ListObjectsRequest(){ 
                    BucketName = sourceBucket,
                    Prefix = sourcePrefix.ToString(),
                    Delimiter = "/"});
    
                // copy each file
                foreach (var file in listObjectsResult.S3Objects)
                {
                    var request = new CopyObjectRequest();
                    request.SourceBucket = Settings.BucketName;
                    request.SourceKey = file.Key;
                    request.DestinationBucket = destinationBucket;
                    request.DestinationKey = destinationPrefix + file.Key.Substring(sourcePrefix.Length);
                    request.CannedACL = S3CannedACL.PublicRead;
                    var response = (CopyObjectResponse)client.CopyObject(request);
                }
    
                // copy subfolders
                foreach (var folder in listObjectsResult.CommonPrefixes)
                {
                    var actualFolder = folder.Substring(sourcePrefix.Length);
                    actualFolder = actualFolder.Substring(0, actualFolder.Length - 1);
                    CopyFolderInsideS3Bucket(strippedSource + "/" + actualFolder, strippedDestination + "/" + actualFolder);
                }
    
                return true;
            }
    

    【讨论】:

      【解决方案2】:

      您可以在亚马逊版本 3.1.5 .net 3.5 这个类的 copyTo 方法中使用 S3DirectoryInfo 类。

      我使用以下示例 C# 代码通过 C# .net 3.5 和亚马逊版本 3.1.5 从 NTFS 文件路径复制到 AmazonS3:

         BasicAWSCredentials basicCredentials = new BasicAWSCredentials("your access key", "your secret key");
                  AmazonS3Config configurationClient = new AmazonS3Config();
                  configurationClient.RegionEndpoint = RegionEndpoint.EUCentral1;
                  try
                  {
                      using (AmazonS3Client clientConnection = new AmazonS3Client(basicCredentials, configurationClient))
                      {
                          S3DirectoryInfo source = new S3DirectoryInfo(clientConnection, "sourcebucketname", "sourcefolderkey");
                          S3DirectoryInfo target = new S3DirectoryInfo(clientConnection, "sourcebucketname", "destinationfolderkey");
                          source.CopyTo(target);
      
                      }
                  }
                  catch(Exception ex)
                  {
      
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-29
        • 1970-01-01
        • 1970-01-01
        • 2020-06-14
        • 1970-01-01
        • 1970-01-01
        • 2014-06-15
        相关资源
        最近更新 更多