如何从Amazon S3下载文件? [英] How to download files from Amazon S3?
问题描述
我在名为BucketA
的存储桶中有一个名为output
的文件夹.我在output
文件夹中有文件列表.如何使用AWS Java SDK将它们下载到我的本地计算机上?
I have a folder named output
inside a bucket named BucketA
. I have a list of files in output
folder. How do I download them to my local machine using AWS Java SDK ?
下面是我的代码:
AmazonS3Client s3Client = new AmazonS3Client(credentials);
File localFile = new File("/home/abc/Desktop/AmazonS3/");
s3Client.getObject(new GetObjectRequest("bucketA", "/bucketA/output/"), localFile);
我得到了错误:
AmazonS3Exception: The specified key does not exist.
推荐答案
请记住,S3不是文件系统,但它是对象存储.两者之间存在巨大差异,一个是目录式活动根本无法正常工作.
Keep in mind that S3 is not a filesystem, but it is an object store. There's a huge difference between the two, one being that directory-style activities simply won't work.
假设您有一个S3存储桶,其中有两个对象:
Suppose you have an S3 bucket with two objects in it:
/path/to/file1.txt
/path/to/file2.txt
使用这些对象时,不能像使用文件系统目录中的文件那样简单地引用/path/to/
.这是因为/path/to/
不是目录,而只是很大的哈希表中键的一部分.这就是为什么错误消息指示 key 出现问题的原因.这些不是文件名路径,而是对象库中对象的键.
When working with these objects you can't simply refer to /path/to/
like you can when working with files in a filesystem directory. That's because /path/to/
is not a directory but just part of a key in a very large hash table. This is why the error message indicates an issue with a key. These are not filename paths but keys to objects within the object store.
要在/path/to/
之类的位置复制所有文件,您需要分多个步骤执行.首先,您需要获取键以/path/to
开头的所有对象的列表,然后需要遍历每个单独的对象并逐个复制它们.
In order to copy all the files in a location like /path/to/
you need to perform it in multiple steps. First, you need to get a listing of all the objects whose keys begin with /path/to
, then you need to loop through each individual object and copy them one by one.
此处是类似的问题,其答案显示了如何使用Java从S3下载多个文件.
Here is a similar question with an answer that shows how to download multiple files from S3 using Java.
这篇关于如何从Amazon S3下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!