简体   繁体   English

如何从Amazon S3下载文件?

[英]How to download files from Amazon S3?

I have a folder named output inside a bucket named BucketA . 我在一个名为BucketA的存储桶中有一个名为output的文件夹。 I have a list of files in output folder. 我有一个output文件夹中的文件列表。 How do I download them to my local machine using AWS Java SDK ? 如何使用AWS Java SDK将它们下载到本地计算机?

Below is my code: 以下是我的代码:

AmazonS3Client s3Client = new AmazonS3Client(credentials);
        File localFile = new File("/home/abc/Desktop/AmazonS3/");
        s3Client.getObject(new GetObjectRequest("bucketA", "/bucketA/output/"), localFile);

And I got the error: 我收到了错误:

AmazonS3Exception: The specified key does not exist.

Keep in mind that S3 is not a filesystem, but it is an object store . 请记住,S3不是文件系统,但它是一个对象存储 There's a huge difference between the two, one being that directory-style activities simply won't work. 两者之间存在巨大差异,一个是目录式活动根本不起作用。

Suppose you have an S3 bucket with two objects in it: 假设您有一个带有两个对象的S3存储桶:

/path/to/file1.txt
/path/to/file2.txt

When working with these objects you can't simply refer to /path/to/ like you can when working with files in a filesystem directory. 使用这些对象时,在处理文件系统目录中的文件时,不能简单地引用/path/to/ like。 That's because /path/to/ is not a directory but just part of a key in a very large hash table. 这是因为/path/to/不是目录,而只是非常大的哈希表中键的一部分。 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. 这些不是文件名路径,而是对象库中对象的键。

In order to copy all the files in a location like /path/to/ you need to perform it in multiple steps. 要将所有文件复制到像/path/to/这样的位置/path/to/您需要在多个步骤中执行它。 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. 首先,您需要获取其键以/path/to开头的所有对象的列表,然后您需要遍历每个单独的对象并逐个复制它们。

Here is a similar question with an answer that shows how to download multiple files from S3 using Java. 这是一个类似的问题 ,答案显示了如何使用Java从S3下载多个文件。

I know this question was asked longtime ago, but still this answer might help some one. 我知道很久以前就问过这个问题了,但这个答案可能对某些人有所帮助。

You might want to use something like this to download objects from S3 您可能希望使用类似的东西从S3下载对象

  new ListObjectsV2Request().withBucketName("bucketName").withDelimiter("delimiter").withPrefix("path/to/image/"); 

as mentioned in the S3 doc 正如S3 doc中提到的那样

delimiter be "/" and prefix be your "folder like structure" . 分隔符“/”前缀“文件夹类似结构”

You can use the predefined classes for upload directory and download directory 您可以使用预定义的类来上载目录和下载目录

For Download 下载

MultipleFileDownload xfer = xfer_mgr.downloadDirectory(
    bucketName, key, new File("C:\\Users\\miracle\\Deskto\\Downloads"));

For Upload 上传

MultipleFileUpload xfer = xfer_mgr.uploadDirectory(bucketName, key,Dir,true);

The error message means that the bucket (in this case "bucketA") does not contain a file with the name you specified (in this case "/bucketA/output/"). 错误消息表示存储桶(在本例中为“bucketA”)不包含具有您指定名称的文件(在本例中为“/ bucketA / output /”)。

When you specify the key, do not include the bucket name in the key. 指定密钥时,请勿在密钥中包含存储桶名称。 S3 supports "folders" in the key, which are delimited with "/", so you probably do not want to try to use keys that end with "/". S3支持键中的“文件夹”,用“/”分隔,因此您可能不想尝试使用以“/”结尾的键。

If your bucket "bucketA" contains a file called "output", you probably want to say 如果您的存储桶“bucketA”包含一个名为“output”的文件,您可能想说

new GetObjectRequest("bucketA", "output")

If this doesn't work, other things to check: 如果这不起作用,还要检查其他事项:

  • Do the credentials you are using have permission to read from the bucket? 您使用的凭据是否有权从存储桶中读取?
  • Did you spell all the names correctly? 你拼错了所有的名字吗?

You might want to use listObjects("bucketA") to verify what the bucket actually contains (as seen with the credentials you are using). 您可能希望使用listObjects("bucketA")来验证存储桶实际包含的内容(如您使用的凭据所示)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM