Java 如何将下载的文件对象从 S3 存储到本地目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21229493/
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
How to store a downloaded file object from S3 to a local directory
提问by Shyamala Selvaganapathy
I'm trying to download a file from S3 using AWS SDK for Java and store the particular file in a local directory in my PC.
我正在尝试使用适用于 Java 的 AWS 开发工具包从 S3 下载文件,并将特定文件存储在我 PC 的本地目录中。
The code I wrote for downloading the object is:
我为下载对象而编写的代码是:
public void download(String key) {
S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,key));
}
But what I actually want to do is to pass the local path as a parameter instead of key and store the downloaded file obj in that particular directory say /tmp/AWSStorage/ in my linux box.
但我真正想做的是将本地路径作为参数而不是键传递,并将下载的文件 obj 存储在该特定目录中,例如 /tmp/AWSStorage/ 在我的 linux 框中。
Can you please suggest a way of doing it?
你能建议一种方法吗?
采纳答案by Tej Kiran
In the line S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,key));
bucketname is the S3BucketName and Key is the object name, it is not a local file path.
Key - is the combination of common-prefix / objectname
行中的S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,key));
bucketname 是S3BucketName,Key 是对象名称,它不是本地文件路径。Key - 是 common-prefix / objectname 的组合
i.e. if you file is saved at root of bucket then only name of the object will be the key i.e. myfile.txt but if your file is save like myfolder1/myfolder2/myfile.txt then myfolder1/myfolder is your common-prefix and myfile.txt is objectname.
即,如果您的文件保存在存储桶的根目录下,那么只有对象的名称才是关键,即 myfile.txt 但如果您的文件保存为 myfolder1/myfolder2/myfile.txt,那么 myfolder1/myfolder 是您的通用前缀和 myfile。 txt 是对象名。
S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,"myfolder1/myfolder2/myfile.txt"));
回答by Shiju K Babu
You can use obj.getDataInputStream()
to get the file. And then org.apache.commons.io.IOUtils
copy
method to copy.
您可以使用obj.getDataInputStream()
来获取文件。然后org.apache.commons.io.IOUtils
copy
方法复制。
S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,key));
File file=new File("/tmp/AWSStorage/"+key);
// if the directory does not exist, create it
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
And then you can use either of following.
然后您可以使用以下任一方法。
try {
IOUtils.copy(obj.getDataInputStream(), new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
OR
或者
BufferedReader reader=null;
BufferedWriter out=null;
String data = null;
try {
reader = new BufferedReader(new InputStreamReader(fileObj.getDataInputStream()));
out = new BufferedWriter (new FileWriter(file));
while ((data = reader.readLine()) != null) {
out.write(data);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
reader.close();
out.close();
}
回答by Shyamala Selvaganapathy
I used:
我用了:
s3Client.getObject(new GetObjectRequest(bucket,key),file);
It worked fine.
它工作得很好。
回答by Sagar Chilukuri
With Java >=1.6, you can directly copy the files downloaded to local directory without any problem of file corruption. Check the code:
Java>=1.6,可以直接将下载的文件复制到本地目录,不会出现文件损坏的问题。检查代码:
S3Object fetchFile = s3.getObject(new GetObjectRequest(bucketName, fileName));
final BufferedInputStream i = new BufferedInputStream(fetchFile.getObjectContent());
InputStream objectData = fetchFile.getObjectContent();
Files.copy(objectData, new File("D:\" + fileName).toPath()); //location to local path
objectData.close();
With Java 1.6 and above, you can directly specify path in Files.copy function.
对于 Java 1.6 及以上版本,您可以直接在 Files.copy 函数中指定路径。