All Products
Search
Document Center

Object Storage Service:Simple download using OSS SDK for iOS

Last Updated:Mar 20, 2026

Use the OSS SDK for iOS to download an object to memory as NSData or directly to a local file.

Prerequisites

Before you begin, ensure that you have:

  • An OSSClient instance initialized with a custom domain name or Security Token Service (STS). See Initialization (iOS SDK)

  • The required permissions granted to your RAM user or RAM role (see Permissions below)

Note

The endpoint you specify during initialization determines the bucket region.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. The account owner or administrator must grant access through RAM Policy or Bucket policies.

APIActionWhen required
GetObjectoss:GetObjectAlways required to download an object
GetObjectoss:GetObjectVersionRequired when specifying an object version with versionId
GetObjectkms:DecryptRequired when the object metadata contains X-Oss-Server-Side-Encryption: KMS

Download an object

Download to memory

The following example downloads an object and returns the data as NSData via getResult.downloadedData.

OSSGetObjectRequest *request = [OSSGetObjectRequest new];

// Required: bucket name and full object key (must not include the bucket name)
request.bucketName = @"examplebucket";
request.objectKey  = @"exampledir/exampleobject.txt";

// Optional: track download progress
request.downloadProgress = ^(int64_t bytesWritten,
                              int64_t totalBytesWritten,
                              int64_t totalBytesExpectedToWrite) {
    // bytesWritten                  — bytes downloaded in this chunk
    // totalBytesWritten             — total bytes downloaded so far
    // totalBytesExpectedToWrite     — total size of the object
    NSLog(@"%lld, %lld, %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
};

OSSTask *getTask = [client getObject:request];

[getTask continueWithBlock:^id(OSSTask *task) {
    if (!task.error) {
        OSSGetObjectResult *getResult = task.result;
        NSLog(@"download object success! data: %@", getResult.downloadedData);
    } else {
        NSLog(@"download object failed, error: %@", task.error);
    }
    return nil;
}];

Download to a local file

To save the object directly to disk, set downloadToFileURL on the request. Replace <local-file-path> with the absolute path of the destination file (for example, /var/mobile/Containers/Data/tmp/exampleobject.txt).

OSSGetObjectRequest *request = [OSSGetObjectRequest new];
request.bucketName      = @"examplebucket";
request.objectKey       = @"exampledir/exampleobject.txt";
request.downloadToFileURL = [NSURL fileURLWithPath:@"<local-file-path>"];

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;
}];

Range download

To download a portion of an object, set a byte range on the request.

// Download bytes 0-99 (inclusive)
request.range = [[OSSRange alloc] initWithStart:0 withEnd:99];

Pause and cancel a download

[client getObject:request] returns an OSSTask. Use it to wait for completion or cancel the request.

// Block the current thread until the download completes
[getTask waitUntilFinished];

// Cancel the download
[request cancel];

What's next