应对AmazonS3桶(C#)文件夹里面

编程入门 行业动态 更新时间:2024-10-16 00:25:35
本文介绍了应对AmazonS3桶(C#)文件夹里面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要复制一个文件夹,里面坐了所有现有文件,以AmazonS3的同一个桶里面的另一个文件夹。

I want to copy one folder, with all existing files inside it, to another folder inside AmazonS3's same Bucket.

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

I can copy one object but what i need is to copy folder, with all files, into another Folder.

推荐答案

下面是复制AmazonS3斗这对我的作品里面的文件夹中的例子。 有关详细信息,您可以检查此链接

Here is the example to copy folder inside AmazonS3 Bucket Which works for me. For more details you can check this 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; }

更多推荐

应对AmazonS3桶(C#)文件夹里面

本文发布于:2023-11-03 19:01:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555833.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!