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

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

问题描述

这是一个基本的问题,不是很先进,但我有点卡住了。

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

我想获得的第n个字节托管在S3的文件。据我所知,基本构建块的问题。我知道如何获得的字节数。下面是从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的URL,这样的:

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网址的GET请求的例子的组成部分。帮助是AP preciated!

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:你能向我解释什么的关键是指???

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

推荐答案

如果您使用AWS SDK for Java中,你只需设置的<一个范围href="http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/GetObjectRequest.html#withRange%28long,%20long%29"相对=nofollow> GetObjectRequest 。这里是一个code例如:

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.

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

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