c# - 使用 API 从 Amazon S3 中删除文件夹

标签 c# amazon-web-services amazon-s3

我正在尝试删除基本上是日期的文件夹中的所有文件。

假设,如果文件夹“08-10-2015”下有100个文件,我不想发送所有这100个文件名,而是想发送文件夹名.

我正在尝试下面的代码,但它对我不起作用。

        DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest();

        multiObjectDeleteRequest.BucketName = bucketName;

        multiObjectDeleteRequest.AddKey(keyName + "/" + folderName + "/");


        AmazonS3Config S3Config = new AmazonS3Config()
        {
            ServiceURL = string.Format(servicehost)
        };

        using (IAmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accesskey, secretkey, S3Config))
        {
            try
            {
                DeleteObjectsResponse response = client.DeleteObjects(multiObjectDeleteRequest);
                Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count);

            }
            catch (DeleteObjectsException e)
            {
                // Process exception.
            }

我正在使用上面的代码,但它不起作用。

最佳答案

我认为您可以使用以下代码删除整个文件夹:

 AmazonS3Config cfg = new AmazonS3Config();
 cfg.RegionEndpoint = Amazon.RegionEndpoint.EUCentral1;
 string bucketName = "your bucket name";
 AmazonS3Client s3Client = new AmazonS3Client("your access key", "your secret key", cfg);
 S3DirectoryInfo directoryToDelete = new S3DirectoryInfo(s3Client, bucketName, "your folder name or full folder key");
 directoryToDelete.Delete(true); // true will delete recursively in folder inside

我正在为 .net 3.5 使用亚马逊 AWSSDK.Core 和 AWSSDK.S3 版本 3.1.0.0。 希望对你有帮助

关于c# - 使用 API 从 Amazon S3 中删除文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33017858/

相关文章:

c# - ContextMenuStrip 未正确调整大小

c# - 编译器魔法 : Why?

amazon-web-services - 如何将目标添加到 CloudFormation 中的网络负载均衡器

java - 什么是 Lambda S3Event 的 AWS SDK V2 类?

amazon-web-services - 如何在Elastic Beanstalk中增加Docker中的ulimit?

android - 使用 teamCity 构建到 s3 后上传 ipa 和 apk

c# - 在.Net core控制台应用程序中使用Azure服务总线

c# - TPL 数据流是并行的或有序的,但不能同时是并行的和有序的

linux - 如何在 AWS S3 存储桶中查找重复文件?

ajax - PUTting 到 S3 时出现 ERR_CONNECTION_RESET