2016-05-04 6 views
2

Ich möchte einen Ordner mit allen vorhandenen Dateien in einen anderen Ordner in AmazonS3 gleichen Eimer kopieren.Coping Ordner in AmazonS3 Bucket (C#)

Ich kann ein Objekt kopieren, aber was ich brauche ist, Ordner mit allen Dateien in einen anderen Ordner zu kopieren.

Antwort

3

Hier ist das Beispiel, Ordner in AmazonS3 Bucket zu kopieren, die für mich funktioniert. Für weitere Informationen können Sie dies überprüfen 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; 
     } 
0

Sie S3DirectoryInfo Klasse in der Amazon-Version 3.1.5 .net 3.5 diese Klasse copyTo Methode verwenden könnte.

habe ich die folgende Beispiel C# -Code von NTFS-Dateipfad 3.1.5 zu AmazonS3 mit C# .net 3.5 und amazon Version kopieren:

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) 
      { 

      } 
Verwandte Themen