All Products
Search
Document Center

Object Storage Service:Download objects as local files using OSS SDK for Android

Last Updated:Mar 20, 2026

Use asyncGetObject to download an object from OSS to a local file on an Android device. The method is non-blocking: the OSS SDK calls your OSSCompletedCallback when the download succeeds or fails.

Prerequisites

Before you begin, ensure that you have:

  • An initialized OSSClient instance. See Initialization for setup options including custom domain and Security Token Service (STS).

  • The required permissions granted to your RAM user or RAM role. See Permissions.

Permissions

Alibaba Cloud accounts have full permissions by default. RAM users and RAM roles have no permissions by default — an Alibaba Cloud account or account administrator must grant them via RAM Policy or bucket policy.

ActionWhen required
oss:GetObjectAlways required to download an object
oss:GetObjectVersionRequired when downloading a specific object version using versionId
kms:DecryptRequired when the object metadata contains X-Oss-Server-Side-Encryption: KMS

Download an object to a local file

The following example constructs a GetObjectRequest, issues an async download, reads the response stream into a buffer, and writes the buffer to a local file.

// Specify the bucket name and the full path of the object in OSS.
// Do not include the bucket name in the object path.
GetObjectRequest get = new GetObjectRequest("examplebucket", "exampledir/exampleobject.txt");

oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
    @Override
    public void onSuccess(GetObjectRequest request, GetObjectResult result) {
        long length = result.getContentLength();
        if (length > 0) {
            byte[] buffer = new byte[(int) length];
            int readCount = 0;
            while (readCount < length) {
                try {
                    readCount += result.getObjectContent().read(buffer, readCount, (int) length - readCount);
                } catch (Exception e) {
                    OSSLog.logInfo(e.toString());
                }
            }
            // Specify the full path of the downloaded object. Example: D:\\localpath\\exampleobject.jpg.
            try {
                FileOutputStream fout = new FileOutputStream("download_filePath");
                fout.write(buffer);
                fout.close();
            } catch (Exception e) {
                OSSLog.logInfo(e.toString());
            }
        }
    }

    @Override
    public void onFailure(GetObjectRequest request, ClientException clientException,
                          ServiceException serviceException) {

    }
});

Replace the following placeholders with actual values:

PlaceholderDescriptionExample
examplebucketThe bucket namemy-android-bucket
exampledir/exampleobject.txtThe full path of the object in OSSimages/photo.jpg

References