简体   繁体   English

在 Get Object S3 请求中通过查询字符串指定字节范围

[英]Specify byte range via query string in Get Object S3 request

I'm familiar with the Range HTTP header;我熟悉范围 HTTP header; however, the interface I'm using to query S3 (an img element's.src property) doesn't allow me to specify HTTP headers.但是,我用来查询 S3 的接口(一个 img 元素的 .src 属性)不允许我指定 HTTP 标头。

Is there a way for me to specify my desired range via a parameter in the query string?有没有办法通过查询字符串中的参数指定我想要的范围?

It doesn't seem like there is, but I'm just holding out a shred of hope before I roll my own solution with ajax requests.似乎没有,但在我使用 ajax 请求推出自己的解决方案之前,我只是抱着一丝希望。

Amazon S3 supports Range GET requests, as do some HTTP servers, for example, Apache and IIS. Amazon S3支持Range GET请求,某些HTTP服务器也是如此,例如Apache和IIS。

How CloudFront Processes Partial Requests for an Object (Range GETs) CloudFront如何处理对象的部分请求(范围GET)

I tried to get my S3 object via cURL: 我试图通过cURL获取我的S3对象:

curl -r 0-1024 https://s3.amazonaws.com/mybucket/myobject -o part1
curl -r 1025-  https://s3.amazonaws.com/mybucket/myobject -o part2
cat part1 part2 > myobject

and AWS SDK for JavaScript: 和AWS SDK for JavaScript:

var s3 = new AWS.S3();
var file = require('fs').createWriteStream('part1');
var params = {
    Bucket: 'mybucket',
    Key: 'myobject',
    Range: 'bytes=0-1024'
};
s3.getObject(params).createReadStream().pipe(file);

These two methods work fine for me. 这两种方法对我来说很好。

AWS SDK for JavaScript API Reference (getObject) AWS SDK for JavaScript API参考(getObject)

Below is the Java Code with AWS V2 SDK下面是 Java 代码与 AWS V2 SDK

format the range as below格式化范围如下

var range = String.format("bytes=%d-%d", start, end);

and pass it in below api with GetObjectRequest builder并使用 GetObjectRequest 构建器将其传递到 api 以下

 ResponseBytes<GetObjectResponse> currentS3Obj = client.getObjectAsBytes(GetObjectRequest.builder().bucket(bucket).key(key).range(range).build());
return currentS3Obj.asInputStream();

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

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