c# - 如何使用 C# 复制 AmazonS3 存储桶中的文件夹?

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

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

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

最佳答案

这是在 AmazonS3 存储桶中复制文件夹的示例,这对我有用。 有关更多详细信息,您可以查看此 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;
        }

关于c# - 如何使用 C# 复制 AmazonS3 存储桶中的文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37028861/

相关文章:

amazon-web-services - 资源处理程序返回消息 : "Your access has been denied by S3, 请确保您的请求凭据有权 GetObject

c# - ConfigurationManager.AppSettings ["SMTP"];空异常

c# - 两个日期之间的天数、小时数、分钟数、秒数

c# - Server.MapPath VS。 @Href ("~/SomeFile.cshtml")

c# - 将 session cookie 设置为 HttpOnly

amazon-web-services - 在极光云形成中启用多可用区

java - AWS Java 删除使用 CreateImage 创建的快照

c# - json 字符串 : take only top level with JSON. NET 的部分序列化

c# - 即使在重新启动机器后,如何将值永久存储在变量中?

asp.net-mvc - 无法加载 Antlr3.Runtime,HRESULT : 0x80131040