java 如何从 Amazon S3 下载文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34314051/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:44:13  来源:igfitidea点击:

How to download files from Amazon S3?

javaamazon-web-servicesamazon-s3

提问by kylas

I have a folder named outputinside a bucket named BucketA. I have a list of files in outputfolder. How do I download them to my local machine using AWS Java SDK ?

我有一个output名为BucketA. 我有文件output夹中的文件列表。如何使用 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.

回答by Bruce P

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:

假设您有一个包含两个对象的 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. 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.

为了复制一个位置中的所有文件,/path/to/您需要分多个步骤执行。首先,您需要获取键以 开头的所有对象的列表/path/to,然后您需要循环遍历每个单独的对象并逐个复制它们。

Here is a similar questionwith an answer that shows how to download multiple files from S3 using Java.

这是一个类似的问题,其答案显示了如何使用 Java 从 S3 下载多个文件。

回答by Pramesh

I know this question was asked longtime ago, but still this answer might help some one.

我知道这个问题很久以前就有人问过了,但这个答案仍然可能对某些人有所帮助。

你可能想使用这样的东西从 S3 下载对象

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

as mentioned in the S3 doc

S3 文档中所述

delimiterbe "/"and prefixbe your "folder like structure".

分隔符“/”前缀是你的“类似结构的文件夹”

回答by janardhan randhi

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);

回答by Rob

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 "/".

指定key时,key中不要包含bucket名称。 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")来验证存储桶实际包含的内容(如您使用的凭证所示)。