简体   繁体   English

在Java中从Amazon S3读取文件的最佳方式

[英]Best way to read a file from Amazon S3 in Java

I am being given a URL to an S3 file in the following format: 我按以下格式获得了一个S3文件的URL:

s3n://XXXXXXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXX@bucket/foo/bar.json S3N:// XXXXXXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXX@bucket/foo/bar.json

What is the best way to go about pulling the json file from this location? 从这个位置拉json文件的最佳方法是什么?

I would like to treat the URL as an opaque value, and avoid parsing it so that it can change with out me needing to change the underlying code. 我想将URL视为一个不透明的值,并避免解析它,以便它可以更改而不需要更改底层代码。

When I attempt to pass the value to java.net.URL it throws a Mal Formed Exception, because it doesn't recognize the S3 protocol. 当我尝试将值传递给java.net.URL时,它会抛出Mal Formed Exception,因为它无法识别S3协议。

Thank you! 谢谢!

You can implement an URLStreamHandler that support the s3n protocol. 您可以实现支持s3n协议的URLStreamHandler。

The Java doc of the URL constructor explain how URLStreamHandle are discovered. URL构造函数的Java文档解释了如何发现URLStreamHandle。

In the URLStreamHandler you will need to do the url parsing to map it to need of the s3 client. 在URLStreamHandler中,您需要执行url解析以将其映射到s3客户端的需要。

But any code using it doesn't have to be dependent on any S3 related element and should work with other storage if needed URLStreamHandler is present. 但是使用它的任何代码都不必依赖于任何与S3相关的元素,并且如果需要URLStreamHandler,则应该与其他存储一起使用。

In Amazon S3 you first need to authenticate to the given bucket and then pull the InputStream of the needed object and then you can do your need. Amazon S3您首先需要对给定的存储桶进行身份验证,然后拉出所需对象的InputStream ,然后您就可以满足需求。

To pull the object via Amazon RestWebService to get the object or to access the object via the URL you need to provide a public access to it. 要通过Amazon RestWebService拉取对象以获取对象或通过URL访问对象,您需要提供对象的公共访问权限。 See here 看到这里

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

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