All Products
Search
Document Center

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

Last Updated:Aug 20, 2024

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.

  • To download an object, you must have the ossGetObject permission. For more information, see Attach a custom policy to a RAM user.

  • 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.

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.