如何从 s3 url 获取(前 n 个字节)文件 [英] How to get (first n bytes of) file from s3 url

查看:14
本文介绍了如何从 s3 url 获取(前 n 个字节)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个基本的问题,不是很高级,但我有点卡住了.

this is a basic question, not very advanced, but I am a bit stuck.

我正在尝试获取托管在 s3 上的文件的前 n 个字节.我了解这个问题的基本组成部分.我知道如何获取字节.这是来自 AmazonS3 的示例

I am trying to get the first n bytes of a file hosted on s3. I understand the basic building block to the issue. I know how to get the bytes. Here is an example from the AmazonS3

GET /example-object HTTP/1.1
Host: example-bucket.s3.amazonaws.com
x-amz-date: Fri, 28 Jan 2011 21:32:02 GMT
Range: bytes=0-9
Authorization: AWS AKIAIOSFODNN7EXAMPLE:Yxg83MZaEgh3OZ3l0rLo5RTX11o=
Sample Response with Specified Range of the Object Bytes  

现在我有一个 s3 网址,如下所示:

Now I have an s3 url, like this:

http://s3.amazonaws.com/bucket-name/foo/1/2/3/4/file.jpg

我如何将其翻译成文档中指定的请求类型?我知道这是补救措施,但我被卡住了,这感觉有点不透明,尽管那可能只是我.

How do I translate this, into the kind of request specified in the docs? I know this is remedial, but I am stuck, this feels somewhat opaque, though that could just be me.

请帮我将 s3 url 解构为 GET 请求示例的组件.感谢帮助!

Please help me deconstruct the s3 url to the components of the GET request example. Help is appreciated!

更新:如果我要使用 GetObjectRequest api,我将在构造函数中用于存储桶和密钥的内容是什么?

UPDATE: If I am to Use the GetObjectRequest api, what would I use for the bucket and key in the constructor?

UPDATE2:换言之就是这样简单

UPDATE2: In other words is it as simple as

// modelled after http://s3.amazonaws.com/foo/
private String s3UrlToBucket(String s3Url)
{
    Pattern pattern = Pattern.compile("^https?://[^/]+/([^/]+)/.*$");
    Matcher matcher = pattern.matcher(s3Url);
    if(matcher.find())
    {
        return matcher.group(1);
    }
    return null;
}

// modelled after http://s3.amazonaws.com/foo/1/2/3/4.jpg
private String s3UrlToKey(String s3Url)
{
    Pattern pattern = Pattern.compile("^https?://[^/]+/[^/]+/(.*)$");
    Matcher matcher = pattern.matcher(s3Url);
    if(matcher.find())
    {
        return matcher.group(1);
    }
    return null;
}

更新 3:你能向我解释一下 key 指的是什么吗???

UPDATE 3: Can you please explain to me what the key refers to???

推荐答案

如果您使用适用于 Java 的 AWS 开发工具包,您只需在 GetObjectRequest.这是一个代码示例:

If you use the AWS SDK for Java, you would just set the range on the GetObjectRequest. Here is a code example:

AmazonS3Client s3Client = new AmazonS3Client();
GetObjectRequest request = new GetObjectRequest("bucket-name", "foo/1/2/3/4/file.jpg");
request.withRange(0, numberOfBytesToGet);
S3Object s3Object = s3Client.getObject(request);
//s3Object.getObjectContent() has a stream to your object.

这篇关于如何从 s3 url 获取(前 n 个字节)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆