[英]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.