Track upload and download progress by attaching a callback to an OSSPutObjectRequest. The callback fires as bytes are sent, giving you the data needed to drive any progress UI.
Prerequisites
Before you begin, make sure you have:
An
OSSClientinstance initialized using a custom domain name or Security Token Service (STS). See Initialization.The
oss:PutObjectpermission on the target bucket. See Grant custom access policies to a RAM user.
The bucket region is determined by the endpoint specified during initialization.
Track upload progress
Set the uploadProgress block on an OSSPutObjectRequest before executing the task. The block receives three values on each invocation: how many bytes were sent this batch, how many bytes have been sent in total, and the full size of the object. Use these values to compute and display progress.
The following example uploads a local file and logs progress on each callback:
OSSPutObjectRequest *put = [OSSPutObjectRequest new];
// Specify the bucket name.
put.bucketName = @"examplebucket";
// Specify the full object path, excluding the bucket name.
put.objectKey = @"exampledir/exampleobject.txt";
// Specify the full local file path.
// If omitted, the SDK looks for the file in the project directory.
put.uploadingFileURL = [NSURL fileURLWithPath:@"<local-file-path>"];
// Track upload progress.
put.uploadProgress = ^(int64_t bytesSent,
int64_t totalByteSent,
int64_t totalBytesExpectedToSend) {
// bytesSent — bytes sent in this batch
// totalByteSent — cumulative bytes sent so far
// totalBytesExpectedToSend — total object size (bytes)
NSLog(@"Progress: %lld / %lld bytes (this batch: %lld)",
totalByteSent, totalBytesExpectedToSend, bytesSent);
};
// Execute the upload and handle the result.
OSSTask *putTask = [client putObject:put];
[putTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSLog(@"upload object success!");
} else {
NSLog(@"upload object failed, error: %@", task.error);
}
return nil;
}];
// Optional: block the current thread until the task finishes.
// [putTask waitUntilFinished];References
Complete sample code for progress bars in object upload: GitHub
Initialize an OSSClient instance: Initialization