How to get (first n bytes of) file from s3 url

2019-02-27 07:50发布

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

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  

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.

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

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

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

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

2条回答
等我变得足够好
2楼-- · 2019-02-27 07:57

If you are struggling to do this in Ruby (which we were for ages) the correct syntax is:

s3_resource.bucket(bucket).object(key).get(range: "bytes=0-3").body.read
查看更多
Root(大扎)
3楼-- · 2019-02-27 08:04

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.
查看更多
登录 后发表回答