如果僅需要文件中的部分數據,您可以使用范圍下載,下載指定范圍內的數據。
注意事項
本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地域的其他阿里云產品訪問OSS,請使用內網Endpoint。關于OSS支持的Region與Endpoint的對應關系,請參見OSS訪問域名、數據中心、開放端口。
本文以從環境變量讀取訪問憑證為例。如何配置訪問憑證,請參見配置訪問憑證。
本文以OSS域名新建OSSClient為例。如果您希望通過自定義域名、STS等方式新建OSSClient,請參見新建OSSClient。
要范圍下載,您必須有
oss:GetObject
權限。具體操作,請參見為RAM用戶授權自定義的權限策略。
指定正常的下載范圍
以下代碼用于指定正常的下載范圍來下載文件。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import java.io.InputStream;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫Bucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫Object完整路徑,例如exampledir/exampleobject.txt。Object完整路徑中不能包含Bucket名稱。
String objectName = "exampledir/exampleobject.txt";
// 填寫Bucket所在地域。以華東1(杭州)為例,Region填寫為cn-hangzhou。
String region = "cn-hangzhou";
// 創建OSSClient實例。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
InputStream in = null;
try {
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName);
// 對于大小為1000 Bytes的文件,正常的字節范圍為0~999。
// 獲取0~999字節范圍內的數據,包括0和999,共1000個字節的數據。如果指定的范圍無效(比如開始或結束位置的指定值為負數,或指定值大于文件大小),則下載整個文件。
getObjectRequest.setRange(0, 999);
// 范圍下載。
OSSObject ossObject = ossClient.getObject(getObjectRequest);
// 讀取數據。
byte[] buf = new byte[1024];
in = ossObject.getObjectContent();
for (int n = 0; n != -1; ) {
n = in.read(buf, 0, buf.length);
}
ossObject.close();
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
// 數據讀取完成后,獲取的流必須關閉,否則會造成連接泄漏,導致請求無連接可用,程序無法正常工作。
if (in != null) {
in.close();
}
}
}
}
流式讀取一次可能無法讀取全部數據。如果您需要流式讀取64 KB的數據,請使用如下的方式多次讀取,直到讀取到64 KB或者文件結束。詳情請參見InputStream.read。
byte[] buf = new byte[1024];
InputStream in = ossObject.getObjectContent();
for (int n = 0; n != -1; ) {
n = in.read(buf, 0, buf.length);
}
in.close();
指定異常的下載范圍
假設現有大小為1000 Bytes的Object,則指定的正常下載范圍應為0~999。如果指定范圍不在有效區間,會導致Range不生效,響應返回值為200,并傳送整個Object的內容。請求不合法的示例及返回說明如下:
若指定了Range: bytes=500~2000,此時范圍末端取值不在有效區間,返回整個文件的內容,且HTTP Code為200。
若指定了Range: bytes=1000~2000,此時范圍首端取值不在有效區間,返回整個文件的內容,且HTTP Code為200。
標準行為范圍下載
在請求中增加請求頭x-oss-range-behavior:standard,則改變指定范圍不在有效區間時OSS的下載行為。假設現有大小為1000 Bytes的Object:
若指定了Range: bytes=500~2000,此時范圍末端取值不在有效區間,返回500~999字節范圍內容,且HTTP Code為206。
若指定了Range: bytes=1000~2000,此時范圍首端取值不在有效區間,返回HTTP Code為416,錯誤碼為InvalidRange。
以下代碼用于標準行為范圍下載。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫Bucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫Object完整路徑,例如exampledir/exampleobject.txt。Object完整路徑中不能包含Bucket名稱。
String objectName = "exampledir/exampleobject.txt";
// 創建OSSClient實例。
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
try {
// 范圍末端取值不在有效區間,返回500~999字節范圍內容,且HTTP Code為206。
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName);
getObjectRequest.setRange(500, 2000);
getObjectRequest.addHeader("x-oss-range-behavior", "standard");
OSSObject ossObject = ossClient.getObject(getObjectRequest);
ossObject.close();
System.out.println("standard get " + "500~2000 "+ "statusCode:" + ossObject.getResponse().getStatusCode());
System.out.println("standard get " + "500~2000 " + "contentLength:" + ossObject.getResponse().getContentLength());
// 范圍首端取值不在有效區間,以下代碼會拋出異常。返回HTTP Code為416,錯誤碼為InvalidRange。
getObjectRequest = new GetObjectRequest(bucketName, objectName);
getObjectRequest.setRange(1000, 2000);
getObjectRequest.addHeader("x-oss-range-behavior", "standard");
OSSObject ossObject2 = ossClient.getObject(getObjectRequest);
ossObject2.close();
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
相關文檔
關于范圍下載的API接口說明,請參見GetObject。