file

Streaming Download

When the downloaded file is too large or the one-time download takes too long, you can process part of the content at a time through streaming download until the file download is completed.

matters needing attention

  • This article takes East China 1 (Hangzhou) extranet endpoint as an example. If you want to access OSS through other AliCloud products in the same region as OSS, please use the intranet endpoint. For the mapping between regions and endpoints supported by OSS, see Access domain name and data center

  • This article takes reading access credentials from environment variables as an example. For how to configure access credentials, see Java Configuration Access Credentials

  • This article takes the OSS domain name creation OSSClient as an example. If you want to create a new OSSClient by customizing the domain name, STS, etc., see New OSSClient

  • To stream download, you must have oss:GetObject jurisdiction. See User defined permission policy for RAM user authorization

Sample code

The following code is used to stream download the exampleobject.txt file in examplebucket.

 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.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class Demo { public static void main(String[] args) throws Exception { //Endpoint takes East China 1 (Hangzhou) as an example. Please fill in other regions according to the actual situation. For information about endpoints corresponding to other regions, see Accessing domain names and data centers. String endpoint = " https://oss-cn-hangzhou.aliyuncs.com "; //Get access credentials from environment variables. Before running this code example, make sure that the environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET have been set. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); //Fill in the bucket name, such as examplebucket. String bucketName = "examplebucket"; //Fill in the full path of the object, such as exampledir/exampleobject.txt. Bucket names cannot be included in the full path of the object. String objectName = "exampledir/exampleobject.txt"; //Create an OSSClient instance. OSS ossClient = new OSSClientBuilder().build(endpoint,  credentialsProvider); try { //The ossObject contains the name of the storage space where the file is located, the file name, the file metadata, and an input stream. OSSObject ossObject = ossClient.getObject(bucketName,  objectName);               //Read the contents of the file. System.out.println("Object content:"); BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent())); while (true) { String line = reader.readLine(); if (line == null) break; System.out.println("\n" + line); } //After the data reading is completed, the obtained stream must be closed, or the connection will leak, resulting in no connection available for the request, and the program cannot work normally. reader.close(); //The ossObject object must be closed after use. Otherwise, the connection will leak, causing no connection available for the request, and the program cannot work properly. 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 (Throwable 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(); } } } }

Related Documents

  • For the complete sample code of streaming download, see GitHub Example

  • For the description of the API interface for streaming download, see GetObject