Quick start

Last Updated: Aug 08, 2017

The basic object upload and download processes are demonstrated below. For details, you can refer to the following directories of this project:

Test resources: Click to view

Demo: Click to view

Step 1. Initialize the OSSClient

We recommend the STS authentication mode on mobile. For details about authentication, refer to the “Access Control” section.

  1. NSString *endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
  2. // We recommend you initialize OSSClient using STS on the mobile terminal. For more authentication modes, refer to the "Access Control" section.
  3. id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:@"AccessKeyId" secretKeyId:@"AccessKeySecret" securityToken:@"SecurityToken"];
  4. client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];

Initialization of upload and download requests through the OSSClient is thread-safe. You can execute multiple tasks in concurrency.

Step 2. Upload a file

Suppose that you have a bucket in the OSS console. An OSSTask will be returned after each SDK operation. You can configure a continuation for the task to achieve asynchronous callback. You can also use the waitUntilFinished to block other requests and wait until the task is finished.

  1. OSSPutObjectRequest * put = [OSSPutObjectRequest new];
  2. put.bucketName = @"<bucketName>";
  3. put.objectKey = @"<objectKey>";
  4. put.uploadingData = <NSData *>; // Upload NSData directly
  5. put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
  6. NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
  7. };
  8. OSSTask * putTask = [client putObject:put];
  9. [putTask continueWithBlock:^id(OSSTask *task) {
  10. if (!task.error) {
  11. NSLog(@"upload object success!");
  12. } else {
  13. NSLog(@"upload object failed, error: %@" , task.error);
  14. }
  15. return nil;
  16. }];
  17. // Wait until the task is finished
  18. // [putTask waitUntilFinished];

Step 3. Download a specified object

The following code downloads a specified object as NSData:

  1. OSSGetObjectRequest * request = [OSSGetObjectRequest new];
  2. request.bucketName = @"<bucketName>";
  3. request.objectKey = @"<objectKey>";
  4. request.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  5. NSLog(@"%lld, %lld, %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  6. };
  7. OSSTask * getTask = [client getObject:request];
  8. [getTask continueWithBlock:^id(OSSTask *task) {
  9. if (!task.error) {
  10. NSLog(@"download object success!");
  11. OSSGetObjectResult * getResult = task.result;
  12. NSLog(@"download result: %@", getResult.downloadedData);
  13. } else {
  14. NSLog(@"download object failed, error: %@" ,task.error);
  15. }
  16. return nil;
  17. }];
  18. // Use a blocking call to wait until the task is finished
  19. // [task waitUntilFinished];
Thank you! We've received your feedback.