简体   繁体   English

使用 Java sdk 删除 aws s3 存储桶中的文件夹

[英]Delete a folder in aws s3 bucket using Java sdk

I have a question, how could I do to delete a folder/directory when calling a delete method?我有一个问题,如何在调用删除方法时删除文件夹/目录?

for example I have those folders with files inside:例如,我有那些包含文件的文件夹: 在此处输入图片说明

So, I have this method that removes everything inside the directory and leaves it empty...所以,我有这个方法可以删除目录中的所有内容并将其留空......

public void deleteFilesFromS3Bucket(Object object) {
        try {
            String objectName = object.getClass().getSimpleName();
            Field privateObjectId = object.getClass().getDeclaredField("id");
            privateObjectId.setAccessible(true);
            Long objectId = (Long) privateObjectId.get(object);
            for(S3ObjectSummary file : s3.listObjects(bucketName, objectName + "-" + objectId).getObjectSummaries()) {
                s3.deleteObject(bucketName, file.getKey());
            }
        } catch (SdkClientException | NoSuchFieldException | IllegalAccessException e) {
            e.getMessage();
        }
    }

But I want to know if there is any way that deleting will remove the directory/folder...但我想知道是否有任何方法可以删除目录/文件夹...

Folders do not actually exist in Amazon S3.文件夹实际上并不存在于 Amazon S3 中。

For example, you could copy a file to: s3://bucket-name/News-9/foo.txt例如,您可以将文件复制到: s3://bucket-name/News-9/foo.txt

This would cause the News-9 directory to magically appear.这将导致News-9目录神奇地出现。 Then, if the object was deleted, the directory will magically disappear.然后,如果该对象被删除,该目录将神奇地消失。

However, if a Folder is created using the Create Folder button in the S3 management console, a zero-length object is created with the name of the Folder .但是,如果使用 S3 管理控制台中的创建文件夹按钮创建文件夹,则会创建一个名为 Folder 的零长度对象 This forces the directory to appear even if there are no objects in the directory (because there is an object there).这迫使目录出现,即使没有对象在该目录(因为一个对象那里)。

Therefore, if a folder/directory appears in S3 with no objects inside it, there is actually a zero-length object with the name of that folder.因此,如果一个文件夹/目录出现在 S3 中,其中没有对象,则实际上有一个具有该文件夹名称的零长度对象。 It can be delete with a normal DeleteObject() call.它可以通过普通的DeleteObject()调用来删除。 This will cause the folder to disappear.这将导致文件夹消失。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 aws-java-sdk 2.0 删除非空 S3 存储桶 - How to delete non empty S3 bucket using aws-java-sdk 2.0 如何使用 AWS Java SDK for S3 查询 AWS S3 存储桶以匹配对象(文件)名称 - how to query AWS S3 bucket for matching objects(files) names using AWS Java SDK for S3 适用于 Java 版本 2 的 AWS 开发工具包 - 删除 S3“文件夹”或删除多个 S3 对象 - AWS SDK for Java version 2 - Delete S3 "folder" or Delete multiple S3 objects 如何使用 Android Sdk 在 aws S3 存储桶中创建一个空文件夹? - How to create an empty folder in aws S3 bucket with using Android Sdk? 使用 AWS SDK 为 Java 创建 Amazon S3 存储桶:线程“主”java.lang.NoClassDefFoundError 中的异常 - Creating an Amazon S3 bucket Using the AWS SDK for Java : Exception in thread "main" java.lang.NoClassDefFoundError 我们如何使用 Java SDK 从 S3 存储桶下载没有文件夹的多个文件 - How can we download multiple files without folder from S3 bucket using Java SDK 如何使用 aws java sdk 将文件从 S3 存储桶从一个区域复制到另一个区域? - How to copy files from S3 bucket from one region to another region using aws java sdk? 如何使用 IAM 角色通过 aws sdk (java) 从 ECS 容器调用 s3 存储桶 - How to call s3 bucket from ECS container via aws sdk (java) by using IAM role 如何使用AWS Java SDK从S3存储桶中的字节创建文件(如果尚不存在) - How to create File from bytes in S3 Bucket if it does not exist yet using AWS Java sdk 将文件上传到 Amazon S3 存储桶。 使用适用于 Java v2 的 AWS 开发工具包 - Issuing uploading file to Amazon S3 Bucket. using the AWS SDK for Java v2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM