This topic describes how to perform a simple download.
Usage notes
Before you use the examples in this topic, create an OSSClient instance using a custom domain name or Security Token Service (STS). For more information, see Initialization (iOS SDK).
The region of the bucket is determined by the region of the endpoint that is specified in the initialization configuration.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket policies.
API | Action | Definition |
GetObject |
| Downloads an object. |
| When downloading an object, if you specify the object version through versionId, this permission is required. | |
| When downloading an object, if the object metadata contains X-Oss-Server-Side-Encryption: KMS, this permission is required. |
Examples
You can download an object to a local file or as NSData.
OSSGetObjectRequest * request = [OSSGetObjectRequest new];
// Specify the bucket name, for example, examplebucket.
request.bucketName = @"examplebucket";
// Specify the full path of the object, for example, exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name.
request.objectKey = @"exampledir/exampleobject.txt";
// Optional.
request.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
// The current segment length, total downloaded length, and total expected length.
NSLog(@"%lld, %lld, %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
};
// request.range = [[OSSRange alloc] initWithStart:0 withEnd:99]; // bytes=0-99. Specifies a range for the download.
// request.downloadToFileURL = [NSURL fileURLWithPath:@"<filepath>"]; // To download the object directly to a file, specify the path of the destination 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 simple download API operation, see GetObject.
For more information about initializing an OSSClient instance, see How to initialize an OSSClient instance.