說明
本文檔可能包含第三方產品資訊,該資訊僅供參考。阿里雲對第三方產品的效能、可靠性以及操作可能帶來的潛在影響,不做任何暗示或其他形式的承諾。
概述
本文主要介紹如何通過HTTP Range請求分段擷取OSS資源。
詳細資料
上傳或者下載OSS中的大檔案(超過100 MB)時,如果傳輸過程中受到網路環境影響,則會傳輸失敗。在上傳過程中,可以調用UploadPart進行分區上傳。而下載資源時,可以通過HTTP Range請求擷取大檔案的部分內容,樣本如下。
Get /ObjectName HTTP/1.1
Host:xxxx.oss-cn-hangzhou.aliyuncs.com
Date:Tue, 17 Nov 2015 17:27:45 GMT
Authorization:SignatureValue
Range:bytes=[$ByteRange]
說明
[$ByteRange]指請求資源的範圍,單位為“Byte(位元組)”,樣本如下:
Range: bytes=0-499
表示第0~499位元組範圍的內容。
Range: bytes=500-999
表示第500~999位元組範圍的內容。
Range: bytes=-500
表示最後500位元組的內容。
Range: bytes=500-
表示從第500位元組開始到檔案結束部分的內容。
Range: bytes=0-
表示第一個位元組到最後一個位元組,即完整的檔案內容。
OSS不支援多Range參數,即不支援指定多個範圍。如果指定多個範圍,OSS只返回第一個Range的資料,例如:Range:bytes=0-499,500-999
,OSS只返回0~499位元組範圍的內容。
[$ByteRange]有效區間在0至content-length - 1
的範圍內。
如果HTTP Range請求合法,響應傳回值為206
並在回應標頭中包含Content-Range
。如果HTTP Range請求不合法,或者指定範圍不在有效區間,會導致Range不生效,響應傳回值為200
,並傳送整個Object內容。如下為HTTP Range請求不合法的樣本及錯誤說明。
說明
此處假設Object資源大小為1000位元組,Range有效區間為0~999。為避免指定的Range超出範圍,可在Range讀取前進行HeadObject請求,擷取對象大小。
Range: byte=0-499
:格式錯誤,byte應為bytes。
Range: bytes=0-1000
:末位元組1000超出有效區間。
Range: bytes=1000-2000
:指定範圍超出有效區間。
Range: bytes=1000-
:首位元組超出有效區間。
Range: bytes=-2000
:指定範圍超出有效區間。
可以通過如下命令測試Range參數的有效性。
curl -r 0-100 http://xxxx.oss-cn-hangzhou.aliyuncs.com/xx.zip -o /tmp/xx1.zip -v
相容行為
使用HTTP Range時,增加要求標頭x-oss-range-behavior:standard
,可以改變指定範圍不在有效區間時OSS的行為。行為改變的樣本如下:
說明
此處假設Object資源大小為1000位元組,Range有效區間為0~999。如通過HTTP Range請求擷取大檔案的部分內容時,因選取了無效的範圍,導致OSS返回InvalidRange錯誤碼,請參見OSS返回416錯誤進行解決,詳細錯誤資訊如下:
The requested range cannot be satisfied
Range: bytes=500-2000
:末位元組超出有效區間,返回500~999位元組範圍內容。
Range: bytes=1000-2000
:首位元組超出有效區間,返回錯誤416 (InvalidRange)
。
Range: bytes=1000-
:首位元組超出有效區間,返回錯誤416 (InvalidRange)
。
Range: bytes=-2000
:指定範圍超出有效區間,返回0~999位元組,即完整的檔案內容。
樣本
針對上述內容,本文提供如下HTTP Range請求樣本。
說明
此處假設Object資源大小為1000位元組,Range有效區間為0~999。
正常請求樣本
超出範圍的請求樣本
相容行為請求樣本
增加x-oss-range-behavior:standard
要求標頭,末位元組超出有效區間,返回500~999位元組範圍的內容。
GET /ObjectName
x-oss-range-behavior: standard
Range: bytes=500-2000
Host: bucket.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 18 Oct 2019 07:02:23 GMT
Authorization: Signature
206 (Partial Content)
content-length: 500
content-range: bytes 500-999/1000
etag: "CACF99600561A31D494569C979E6FB81"
x-oss-request-id: 5DA9637FB3B1C73234CC59EB
date: Fri, 18 Oct 2019 07:02:23 GMT
[500 bytes of object data]
增加x-oss-range-behavior:standard
要求標頭,首位元組超出有效區間,返回416
錯誤。
GET /ObjectName
x-oss-range-behavior: standard
Range: bytes=1000-2000
Host: bucket.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 18 Oct 2019 07:04:23 GMT
Authorization: Signature
416 (Requested Range Not Satisfiable)
content-length: 345
x-oss-request-id: 5DA963F7CEBFAA3931BF91F5
date: Fri, 18 Oct 2019 07:04:23 GMT
content-type: application/xml
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>InvalidRange</Code>
<Message>The requested range cannot be satisfied</Message>
<RequestId>5DA963F7CEBFAA3931BF91F5</RequestId>
<HostId>bucket.oss-cn-hangzhou.aliyuncs.com</HostId>
<ActualObjectSize>1000</ActualObjectSize>
<RangeRequested>bytes=1000-2000</RangeRequested>
</Error>
增加x-oss-range-behavior:standard
要求標頭,指定範圍超出有效區間,返回0~999位元組,即完整的檔案內容。
GET /ObjectName
x-oss-range-behavior: standard
Range: bytes=-2000
Host: bucket.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 18 Oct 2019 07:06:39 GMT
Authorization: Signature
206 (Partial Content)
content-length: 1000
content-range: bytes 0-999/1000
etag: "CACF99600561A31D494569C979E6FB81"
x-oss-request-id: 5DA9647FC4334F3534AF9A83
date: Fri, 18 Oct 2019 07:06:39 GMT
[1000 bytes of object data]
適用於