オブジェクトのデータの一部だけが必要な場合は、部分ダウンロードを使用して指定したコンテンツをダウンロードします。

部分ダウンロードは次のコードを実行します。

//This example uses the endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String objectName = "<yourObjectName>";

// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName);
// Obtain data between the starting byte value of 0 and the maximum byte value of 1,000 inclusive (that is, a total of 1,001 bytes). If the specified value range is invalid, for example, the specified range includes a negative number, or the specified value is larger than the object size, the entire object is downloaded.
getObjectRequest.setRange(0, 1000);

// Start range download.
OSSObject ossObject = ossClient.getObject(getObjectRequest);

// Read data.
byte[] buf = new byte[1024];
InputStream in = ossObject.getObjectContent();
for (int n = 0; n ! = -1; ) {
    n = in.read(buf, 0, buf.length);
}

// If you do not close the reader after the data is read, connection leaks may occur. Consequently, no connections are available and an exception occurs.
rs.close();

//Close your OSSClient.
ossClient.shutdown();

ストリーミングされているときにすべてのデータを一度に読み取ることはできません。 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);
}
rs.close();