java 使用 API 在亚马逊 S3 存储桶中创建文件夹/上传文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35080393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating a folder/uploading a file in amazon S3 bucket using API
提问by Shaonline
I am a total newbie to amazon and java. I am trying two things here.
我是亚马逊和 Java 的新手。我在这里尝试两件事。
1st - I am trying to create a folder in my Amazon S3 bucket that i have already created and have got the credentials for.
第一个 - 我正在尝试在我已经创建并获得凭证的 Amazon S3 存储桶中创建一个文件夹。
2nd - I am trying to upload a file to this bucket.
第二 - 我正在尝试将文件上传到此存储桶。
As per my understanding i can use putObjectRequest() method for acheiving both of my tasks.
根据我的理解,我可以使用 putObjectRequest() 方法来实现我的两个任务。
PutObjectRequest(bucketName, keyName, file)
for uploading a file.
用于上传文件。
I am not sure if i should use this method
我不确定我是否应该使用这种方法
PutObjectRequest(String bucketName, String key, InputStream input,
ObjectMetadata metadata)
for just creating a folder. I am struggeling with InputSteam and ObjectMetadata. I don't know what exactly is this for and how can i use it.
仅用于创建文件夹。我正在努力使用 InputSteam 和 ObjectMetadata。我不知道这到底是做什么用的以及如何使用它。
Any help would be greatly appreciated. :)
任何帮助将不胜感激。:)
采纳答案by Shawn Guo
Yes, you can use PutObjectRequest(bucketName, keyName, file) to achive both task.
是的,您可以使用 PutObjectRequest(bucketName, keyName, file) 来完成这两项任务。
1, create S3 folder
With AWS S3 Java SDK , just add "/" at the end of the key name, it will create empty folder.
1、创建S3文件夹
使用AWS S3 Java SDK,只需在键名末尾添加“/”,就会创建空文件夹。
var folderKey = key + "/"; //end the key name with "/"
Sample code:
示例代码:
final InputStream im = new InputStream() {
@Override
public int read() throws IOException {
return -1;
}
};
final ObjectMetadata om = new ObjectMetadata();
om.setContentLength(0L);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, im, om);
s3.putObject(putObjectRequest);
2, Uploading file Just similar, you can get input stream from your local file.
2、上传文件 类似的,你可以从本地文件中获取输入流。
回答by John Rotenstein
You do not need to create a folder in Amazon S3. In fact, folders do not exist!
您无需在 Amazon S3 中创建文件夹。事实上,文件夹不存在!
Rather, the Key(filename) contains the full path and the object name.
相反,密钥(文件名)包含完整路径和对象名称。
For example, if a file called cat.jpg
is in the animals
folder, then the Key (filename) is: animals/cat.jpg
例如,如果文件夹中有一个名为cat.jpg
的animals
文件,则密钥(文件名)为:animals/cat.jpg
Simply Put
an object with that Key and the folder is automatically created. (Actually, this isn't true because there are no folders, but it's a nice simple way to imagine the concept.)
只需Put
一个带有该密钥的对象,就会自动创建文件夹。(实际上,这不是真的,因为没有文件夹,但这是一个很好的简单方式来想象这个概念。)
As to which function to use... always use the simplest one that meets your needs. Therefore, just use PutObjectRequest(bucketName, keyName, file)
.
至于使用哪个功能...总是使用最简单的满足您需求的功能。因此,只需使用PutObjectRequest(bucketName, keyName, file)
.
回答by koolhead17
Alternatively you can use [minio client]java library
或者,您可以使用[minio client]java 库
You can follow MakeBucket.javaexample to create a bucket & PutObject.javaexample to add an object.
您可以按照MakeBucket.java示例创建存储桶和PutObject.java示例来添加对象。
Hope it help.
希望有帮助。