Download objects

Last Updated: Oct 26, 2017

Simple download

You can download an object to a local path or as NSData.

  1. OSSGetObjectRequest * request = [OSSGetObjectRequest new];
  2. // Required field
  3. request.bucketName = @"<bucketName>";
  4. request.objectKey = @"<objectKey>";
  5. // Optional field
  6. request.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  7. // Length of bytes downloaded, total length of bytes downloaded, and total length of bytes expected to be downloaded
  8. NSLog(@"%lld, %lld, %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  9. };
  10. // request.range = [[OSSRange alloc] initWithStart:0 withEnd:99]; // Download by a specified range of bytes=0-99.
  11. // request.downloadToFileURL = [NSURL fileURLWithPath:@"<filepath>"]; // Specify the target path if the downloaded object needs to be saved as a file
  12. OSSTask * getTask = [client getObject:request];
  13. [getTask continueWithBlock:^id(OSSTask *task) {
  14. if (!task.error) {
  15. NSLog(@"download object success!");
  16. OSSGetObjectResult * getResult = task.result;
  17. NSLog(@"download result: %@", getResult.dowloadedData);
  18. } else {
  19. NSLog(@"download object failed, error: %@" ,task.error);
  20. }
  21. return nil;
  22. }];
  23. // [getTask waitUntilFinished];
  24. // [request cancel];

Image processing

Image processing service is provided to external users by Alibaba Cloud OSS featuring massive processing power, security, low costs, and high reliability.

By uploading and storing source images in OSS, you can process images anytime, anywhere, and on any Internet device through a simple RESTful interface.

The OSS image processing offers image processing interfaces. For image uploads, use the OSS upload interfaces. You can build your own image processing service based on OSS image processing.

OSS image processing provides the following features:

During SDK usage, you must set the xOssProcess parameter for request when downloading images. Example:

  1. OSSGetObjectRequest * request = [OSSGetObjectRequest new];
  2. request.bucketName = @"<bucketName>";
  3. request.objectKey = @"example.jpg";
  4. // Image processing
  5. request.xOssProcess = @"image/resize,m_lfit,w_100,h_100";
  6. OSSTask * getTask = [client getObject:request];
  7. [getTask continueWithBlock:^id(OSSTask *task) {
  8. if (!task.error) {
  9. NSLog(@"download image success!");
  10. OSSGetObjectResult * getResult = task.result;
  11. NSLog(@"download image data: %@", getResult.dowloadedData);
  12. } else {
  13. NSLog(@"download object failed, error: %@" ,task.error);
  14. }
  15. return nil;
  16. }];
  17. // [getTask waitUntilFinished];
  18. // [request cancel];

Stream download

The SDK does not provide any stream download interface, but provides a multipart callback function similar to didReceiveData of the NSURLSession library to notify you that a data part is downloaded.

Note: The download result does not contain the actual data if the multipart callback is configured.

  1. OSSGetObjectRequest * request = [OSSGetObjectRequest new];
  2. // required
  3. request.bucketName = @"<bucketName>";
  4. request.objectKey = @"<objectKey>";
  5. // Multipart callback function
  6. request.onReceiveData = ^(NSData * data) {
  7. NSLog(@"Receive data, length: %ld", [data length]);
  8. };
  9. OSSTask * getTask = [client getObject:request];
  10. [getTask continueWithBlock:^id(OSSTask *task) {
  11. if (!task.error) {
  12. NSLog(@"download object success!");
  13. } else {
  14. NSLog(@"download object failed, error: %@" ,task.error);
  15. }
  16. return nil;
  17. }];
  18. // [getTask waitUntilFinished];
  19. // [request cancel];

Download within a specified range

You can specify the range for downloading an object. This feature applies to large objects. If the Range parameter is specified in the request header, the returned message contains the length of the entire object and the range of the returned object content.

  1. OSSGetObjectRequest * request = [OSSGetObjectRequest new];
  2. request.bucketName = @"<bucketName>;
  3. request.objectKey = @"<objectKey>";
  4. request.range = [[OSSRange alloc] initWithStart:1 withEnd:99]; // bytes=1-99
  5. // request.range = [[OSSRange alloc] initWithStart:-1 withEnd:99]; // bytes=-99
  6. // request.range = [[OSSRange alloc] initWithStart:10 withEnd:-1]; // bytes=10-
  7. request.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  8. NSLog(@"%lld, %lld, %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  9. };
  10. OSSTask * getTask = [client getObject:request];
  11. [getTask continueWithBlock:^id(OSSTask *task) {
  12. if (!task.error) {
  13. NSLog(@"download object success!");
  14. OSSGetObjectResult * getResult = task.result;
  15. NSLog(@"download result: %@", getResult.dowloadedData);
  16. } else {
  17. NSLog(@"download object failed, error: %@" ,task.error);
  18. }
  19. return nil;
  20. }];
  21. // [getTask waitUntilFinished];
  22. // [request cancel];

Retrieve the object metadata only

The headObject method can be used to obtain only the metadata of an object without the object body. The code is as follows:

  1. OSSHeadObjectRequest * request = [OSSHeadObjectRequest new];
  2. request.bucketName = @"<bucketName>;
  3. request.objectKey = @"<objectKey>";
  4. OSSTask * headTask = [client headObject:request];
  5. [headTask continueWithBlock:^id(OSSTask *task) {
  6. if (!task.error) {
  7. NSLog(@"head object success!");
  8. OSSHeadObjectResult * result = task.result;
  9. NSLog(@"header fields: %@", result.httpResponseHeaderFields);
  10. for (NSString * key in result.objectMeta) {
  11. NSLog(@"ObjectMeta: %@ - %@", key, [result.objectMeta objectForKey:key]);
  12. }
  13. } else {
  14. NSLog(@"head object failed, error: %@" ,task.error);
  15. }
  16. return nil;
  17. }];
Thank you! We've received your feedback.