All Products
Search
Document Center

Object Storage Service:iOS progress bar

Last Updated:Aug 20, 2024

You can use a progress bar to indicate the progress of an object that is being uploaded or downloaded. The GetObject operation is used in the topic to describe how to display the progress bar of an object that is being downloaded.

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.

Examples

The following sample code provides an example on how to display the progress of an object named exampleobject.txt that is being downloaded from a bucket named examplebucket:

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 progress callback function to display the progress bar. 
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);
};

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;
}];
// Implement synchronous blocking to wait for the task to complete. 
// [putTask waitUntilFinished]; 

References

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