All Products
Search
Document Center

Object Storage Service:Simple download

Last Updated:Apr 11, 2025

This topic describes how to perform simple download.

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.

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.

Example

You can download an object as a local file or as NSData:

OSSGetObjectRequest * request = [OSSGetObjectRequest new];

// Specify the name of the bucket. Example: examplebucket. 
request.bucketName = @"examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
request.objectKey = @"exampledir/exampleobject.txt";

// Configure optional fields. 
request.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
    // Specify the number of bytes that are being downloaded, the number of bytes that are downloaded, and the total number of bytes that you want to download. 
    NSLog(@"%lld, %lld, %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
};
// request.range = [[OSSRange alloc] initWithStart:0 withEnd:99]; // bytes=0-99. Specify that the download range is from byte 0 to byte 99. 
// request.downloadToFileURL = [NSURL fileURLWithPath:@"<filepath>"]; // If you need to directly download the object to a file, specify the path of the file. 

OSSTask * getTask = [client getObject:request];

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

// [getTask waitUntilFinished];

// [request cancel];

References

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

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