删除Amazon S3 Java中的文件,目录和存储桶 [英] Delete files, directories and buckets in amazon s3 java
问题描述
我想知道如何做到这一点.我查看了sdk文档并提供了一些示例,但对语法的一般用法感到困惑.
I'm wondering how to do this. I looked at the sdk documentation and have some examples, but am confused how the syntax generally goes.
如果要删除文件,我假设我使用 deleteObject(path,key)
.但是,钥匙"是什么?
If I want to delete a file, I assume I use deleteObject(path, key)
. However, what is the "key"?
还如何删除目录?我似乎找不到执行此操作的方法.
Also how do you delete a directory? I can't seem to find a method for doing that.
推荐答案
S3中的键"类似于文件路径:
A "key" in S3 is similar to a file path:
http://bucket.s3.amazonaws.com/some/path/to/use
...位于名为 bucket
的存储桶中,其密钥为 some/path/to/use
.
... is in a bucket named bucket
and has a key of some/path/to/use
.
它实际上不是路径,因为没有文件夹.S3密钥只是一个大目录(整个存储桶)中文件的文件名.S3密钥可以包含/
,但是除非您设置
It's not actually a path though, because there are no folders. The S3 key is just the file name for a file in one big directory (the entire bucket). S3 keys can contain /
, but it has no special meaning unless you set the delimiter
argument with listing a bucket.
换句话说,拥有一个名为 some/object
的对象不会告诉您有关该对象 some
的任何信息(它可能存在或可能不存在-这两个对象无关).
In other words, having an object named some/object
doesn't tell you anything about the object some
(it might or might not exist -- the two objects are not related).
但是,您可以请求具有特定前缀的密钥,因此我可以说给我所有以 some/path/to/
开头的密钥,它将返回 some/path/to/use
.它看起来像列出目录",但实际上只是在询问以特定字符串开头的文件.
However, you can request keys with a specific prefix, so I could say "give me all keys starting with some/path/to/
and it will return some/path/to/use
. It looks like "listing a directory", but it's really just asking for files that start with a specific string of characters.
我可以很容易地这样命名:
I could just as easily name things like this:
somepathtousea
somepathtouseb
并说给我以 somepathtouse
开头的所有内容"(它会说 somepathtousea
和 somepathtouseb
).
And say "give me everything starting with somepathtouse
" (and it would say somepathtousea
and somepathtouseb
).
注意:S3 URL以几种形式:
Note: S3 URL's come in several forms:
http://s3.amazonaws.com/bucket/key
http://bucket.s3.amazonaws.com/key
http://bucket/key (where bucket is a DNS CNAME record pointing to bucket.s3.amazonaws.com)
我查看了 JavaDocs ,这是我看到的功能签名(对于 AmazonS3Client
):
I looked at the JavaDocs and this is the function signature I see (for AmazonS3Client
):
public void deleteObject(java.lang.String bucketName,
java.lang.String key)
throws AmazonClientException,
AmazonServiceException
再次
文件夹确实存在,它们是零长度的对象,其内容类型为 application/x-directory
,并且键的结尾为/
:>
Folders do kind-of exist now, as zero-length objects with a content-type of application/x-directory
and a key ending in /
:
$ AWS_PROFILE=prod aws s3api head-object --bucket example-bucket --key example-directory/
{
"AcceptRanges": "bytes",
"LastModified": "Mon, 29 Apr 2019 14:59:36 GMT",
"ContentLength": 0,
"ETag": "\"d41d8cd98f00b204e9800998ecf8427e\"",
"ContentType": "application/x-directory",
"ServerSideEncryption": "AES256",
"Metadata": {}
}
这仍然只是惯例,没有什么可以阻止您以/
结尾的文件或不存在的文件夹"中的文件.
This is still just convention and there's nothing stopping you from having files ending /
or files inside of "folders" that don't exist.
这篇关于删除Amazon S3 Java中的文件,目录和存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!