All Products
Search
Document Center

Object Storage Service:Streaming download by using OSS SDK for iOS

Last Updated:Apr 11, 2025

If you need to download a large object or if the download requires a long period of time to complete, you can perform streaming download to download the object in increments.

Usage notes

  • Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.

    Note

    The region of the bucket is determined by the region of the endpoint specified for initialization.

  • OSS SDK for iOS does not provide any streaming download API operations. Instead, it provides the multipart callback feature that is similar to the didRecieveData function in the NSURLSession library. The download result does not contain the actual data if multipart callback is configured.

Permissions

By default, an Alibaba Cloud account has full permissions on resources in the account. In contrast, RAM users and RAM roles associated with an Alibaba Cloud account initially have no permissions. To manage resources by using a RAM user or role, you must grant the required permissions via RAM policies or Bucket policies.

API

Action

Description

GetObject

oss:GetObject

Grants the permission to download an object.

oss:GetObjectVersion

Grants the permission to query object versions. This permission is required if you want to download a specific version of an object.

kms:Decrypt

Grants the permission to use Key Management Service (KMS) decryption. This permission is required if you want to download an object in parts that you encrypt by specifying the x-oss-server-side-encryption header.

Examples

The following sample code provides an example on how to download an object by performing streaming download:

OSSGetObjectRequest * request = [OSSGetObjectRequest new];
// Specify the name of the bucket. Example: examplebucket. 
request.bucketName = @"examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
request.objectKey = @"exampledir/exampleobject.txt";
// Configure the multipart callback function. 
request.onRecieveData = ^(NSData * data) {
    NSLog(@"Recieve data, length: %ld", [data length]);
};
OSSTask * getTask = [client getObject:request];
[getTask continueWithBlock:^id(OSSTask *task) {
    if (!task.error) {
        NSLog(@"download object success!");
    } else {
        NSLog(@"download object failed, error: %@" ,task.error);
    }
    return nil;
}];
// [getTask waitUntilFinished];
// [request cancel];

References

  • For more information about the API operation that you can call to perform streaming download, see GetObject.

  • For more information about how to initialize an OSSClient instance, see Initialization.