No Description

OssViewController.m 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // OssViewController.m
  3. // campus
  4. //
  5. // Created by zack on 2018/5/29.
  6. // Copyright © 2018年 Facebook. All rights reserved.
  7. //
  8. #import "OssViewController.h"
  9. #import <AliyunOSSiOS.h>
  10. @interface OssViewController ()
  11. @property(strong, nonatomic) OSSClient * client;
  12. @property(strong, nonatomic) NSMutableArray * imageNames;
  13. @property(strong, nonatomic) NSMutableArray * imageBase64Datas;
  14. @end
  15. #define akid @"LTAIbKJk7bY1JTm4"
  16. #define skid @"UWHW9eC2EYz6Uj391OPQt4LM9rLuzC"
  17. #define endpoint @"https://oss-cn-hangzhou.aliyuncs.com"
  18. #define token @"CAES+wMIARKAAZhjH0EUOIhJMQBMjRywXq7MQ/cjLYg80Aho1ek0Jm63XMhr9Ocer8p1YaX1NTDiCFZWFkvlHf1pQhuxfKBc+mRR9KAbHUefqH+rdjZqjTF7p2m1wJXP8S6k+G2MpHrUe6TYBkJ43GhhTVFMuM3BZajY3VjZWOXBIODRIR1FKZjIiEjMzMzE0MjY0NzM5MTE4NjkxMSoLY2xpZGSSDgSDGAGESGTETqOio6c2RrLWRlbW8vKgoUYWNzOm9zczoqOio6c2RrLWRlbW9KEDExNDg5MzAxMDcyNDY4MThSBTI2ODQyWg9Bc3N1bWVkUm9sZVVzZXJgAGoSMzMzMTQyNjQ3MzkxMTg2OTExcglzZGstZGVtbzI="
  19. @implementation OssViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view from its nib.
  23. self.view.backgroundColor = [UIColor whiteColor];
  24. }
  25. - (void)initAndUploadImage:(NSArray *)imageNames imageBase64Datas:(NSArray *)imageBase64Datas {
  26. [self.imageNames addObjectsFromArray:imageNames];
  27. [self.imageBase64Datas addObjectsFromArray:_imageBase64Datas];
  28. [self benginUploadTask];
  29. }
  30. - (void)benginUploadTask {
  31. NSOperationQueue * queue = [[NSOperationQueue alloc] init];
  32. NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
  33. //NSLog(@"%@", [NSThread currentThread]);
  34. }];
  35. for (NSInteger i = 0; i < 5; i ++) {
  36. [operation addExecutionBlock:^{
  37. NSData *imageData = [[NSData alloc] initWithBase64EncodedString:self.imageBase64Datas[i] options:0];
  38. [self uploadWithImageName:self.imageNames[i] imageData:imageData];
  39. }];
  40. }
  41. [operation setCompletionBlock:^{
  42. NSLog(@"all ok");
  43. }];
  44. [queue addOperation:operation];
  45. }
  46. - (void)uploadWithImageName:(NSString *)imageName imageData:(NSData *)imageData{
  47. OSSPutObjectRequest * put = [OSSPutObjectRequest new];
  48. put.bucketName = @"links123-images";
  49. put.objectKey = imageName;
  50. put.uploadingData = imageData; // 直接上传NSData
  51. put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
  52. NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
  53. };
  54. //https://links123-images.oss-cn-hangzhou.aliyuncs.com/test.png
  55. OSSTask * putTask = [self.client putObject:put];
  56. [putTask waitUntilFinished];
  57. if (!putTask.error) {
  58. NSLog(@"okay");
  59. }else {
  60. NSLog(@"failed");
  61. }
  62. }
  63. - (OSSClient *)client {
  64. if (!_client) {
  65. id<OSSCredentialProvider> provider = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:akid secretKeyId:skid securityToken:@""];
  66. _client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:provider];
  67. }
  68. return _client;
  69. }
  70. - (NSMutableArray *)imageNames {
  71. if (_imageNames) {
  72. _imageNames = [[NSMutableArray alloc] init];
  73. }
  74. return _imageNames;
  75. }
  76. - (NSMutableArray *)imageBase64Datas {
  77. if (_imageBase64Datas) {
  78. _imageBase64Datas = [[NSMutableArray alloc] init];
  79. }
  80. return _imageBase64Datas;
  81. }
  82. @end