Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 views
in Technique[技术] by (71.8m points)

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

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???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...