Java 如何使用提供的 url 从 s3 下载文件?

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

How to download a file from s3 using provided url?

javaamazon-web-servicesamazon-s3aws-java-sdk

提问by Rohit Chouhan

In my application I will get the url of s3 file like : https://s3.amazonaws.com/account-update/input.csvI have to download it and then process it. What I already done :

在我的应用程序中,我将获得 s3 文件的 url,如:https: //s3.amazonaws.com/account-update/input.csv我必须下载它然后处理它。我已经做了什么:

AmazonS3 s3 = new AmazonS3Client(credentials);
S3Object s3object = s3.getObject(new GetObjectRequest(
bucketName, key));

I am able to download the file by providing bucket name and key, but how can I download the file using the url(https://s3.amazonaws.com/account-update/input.csv) only?

我可以通过提供存储桶名称和密钥来下载文件,但是如何仅使用 url( https://s3.amazonaws.com/account-update/input.csv)下载文件?

回答by ITAdminNC

To enable access by HTTP, you must set the bucket up as a Static Websitein the S3 console. Be warned, this will expose all of your data to the web unless you set up proper S3 access controls.

要启用 HTTP 访问,您必须在 S3 控制台中将存储桶设置为静态网站。请注意,除非您设置了适当的S3 访问控制,否则这会将您的所有数据暴露给网络

The method you are accessing via the Java SDK will not use this type of connection, though. It will connect via the built-in S3 protocol. You should inspect your IAM Role or Policy to ensure you have the correct permissions (s3:GetObject). You will also need s3:ListBucketto see a 404 if the object does not exist.

但是,您通过 Java SDK 访问的方法不会使用这种类型的连接。它将通过内置的 S3 协议进行连接。您应该检查您的 IAM 角色或策略以确保您拥有正确的权限 ( s3:GetObject)。s3:ListBucket如果对象不存在,您还需要看到 404。

回答by John Rotenstein

You could download the file via a standard curl/wget, the same as you would download any other file off the Internet.

您可以通过标准 curl/wget 下载文件,就像从 Internet 下载任何其他文件一样。

The important part, however, is to enable access to the objectfrom Amazon S3. A few options:

然而,重要的部分是允许从 Amazon S3访问对象。几个选项:

  • Make the object publicly-readable:This can be done via the console or CLI/API. However, anyone with that URL will be able to download it.
  • Create an Amazon S3 Bucket Policythat grants read access for the desired file/directory/bucket. But, again, anyone with the URL will be able to access those objects.
  • Keep the object private, but use a pre-signed URLthat adds parameters to the URL to prove that you are permitted to download the object. This pre-signed URL is time-limited and can be generated with a few lines of code using current AWS credentials.
  • 使对象公开可读:这可以通过控制台或 CLI/API 来完成。但是,任何拥有该 URL 的人都可以下载它。
  • 创建Amazon S3 存储桶策略,授予对所需文件/目录/存储桶的读取访问权限。但是,同样,任何拥有 URL 的人都可以访问这些对象。
  • 保持对象私有,但使用预先签名的 URL,该 URL 向URL添加参数以证明您有权下载该对象。这个预先签名的 URL 是有时间限制的,可以使用当前的 AWS 凭证通过几行代码生成。

回答by Chris Turner

John Rotensteinis correct, you can download the file via the URL using standard curl/wget.

John Rotenstein是正确的,您可以使用标准 curl/wget 通过 URL 下载文件。

If you wanted to do this using Java, something like the following should do the trick; making use of the Apache HttpComponentspackage

如果你想使用 Java 来做到这一点,下面的代码应该可以解决问题;使用Apache HttpComponents

private void downloadRequest(String url, String savedFile) {
    HttpClient client = HttpClients.createDefault();

    HttpGet httpGet = new HttpGet(url);
    HttpResponse response;
    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            try (FileOutputStream outstream = new FileOutputStream(savedFile)) {
                entity.writeTo(outstream);
            } catch (IOException e) {
                LOGGER.info(e.getMessage());
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
} 

回答by Jawad

You may consider using the AWS SDK class AmazonS3URIas shown below:

您可以考虑使用 AWS SDK 类AmazonS3URI,如下所示:

URI fileToBeDownloaded = new URI(" https://s3.amazonaws.com/account-update/input.csv"); 

AmazonS3URI s3URI = new AmazonS3URI(fileToBeDownloaded);

S3Object s3Object = s3Client.getObject(s3URI.getBucket(), s3URI.getKey());

From here on, you should be able to utilise the s3Object obtained in a similar fashion to the s3Object shown in your code.

从这里开始,您应该能够使用以与代码中显示的 s3Object 类似的方式获得的 s3Object。

For more Java related AWS SDK examples on using this class check here

有关使用此类的更多 Java 相关 AWS SDK 示例,请在此处查看

回答by tooptoop4

using cli: aws s3 cp s3://bucket/prefix/key targetlocalfolder

使用 cli:aws s3 cp s3://bucket/prefix/key targetlocalfolder

回答by Reza Mousavi

The best way is using the pre-signed S3 URL to achieve your needs. You can add expiration time to your signed URLs and after that the URL not available.

最好的方法是使用预先签名的 S3 URL 来满足您的需求。您可以将过期时间添加到您的签名 URL,然后该 URL 不可用。

For more information read the following page:

有关更多信息,请阅读以下页面:

https://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURLJavaSDK.html

https://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURLJavaSDK.html

回答by Alberto Cerqueira

You can't, but you can make the file attachment in the upload.

你不能,但你可以在上传中制作文件附件。

For example:

例如:

ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType("application/csv;charset=utf-8");
objectMetadata.setContentDisposition("attachment");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, arquivo, file, objectMetadata).withCannedAcl(CannedAccessControlList.PublicRead);
amazonS3.putObject(putObjectRequest);

I hope it helps.

我希望它有帮助。

回答by Riya John

Just enter the url on the browser but make sure to replace \u0026with &if you have downloaded the url via curl else you will get this error

只需在浏览器上输入网址,但如果您通过 curl 下载了网址,请确保替换为\u0026&否则您将收到此错误

<Error>
    <Code>AuthorizationQueryParametersError</Code>
    <Message>X-Amz-Algorithm only supports "AWS4-HMAC-SHA256"</Message>
</Error>
<Error>
    <Code>AuthorizationQueryParametersError</Code>
    <Message>X-Amz-Algorithm only supports "AWS4-HMAC-SHA256"</Message>
</Error>