123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- //
- // OssViewController.m
- // campus
- //
- // Created by zack on 2018/5/29.
- // Copyright © 2018年 Facebook. All rights reserved.
- //
-
- #import "OssViewController.h"
- #import <AliyunOSSiOS.h>
-
- @interface OssViewController ()
- @property(strong, nonatomic) OSSClient * client;
- @property(strong, nonatomic) NSMutableArray * imageNames;
- @property(strong, nonatomic) NSMutableArray * imageBase64Datas;
- @end
-
- #define akid @"LTAIbKJk7bY1JTm4"
- #define skid @"UWHW9eC2EYz6Uj391OPQt4LM9rLuzC"
- #define endpoint @"https://oss-cn-hangzhou.aliyuncs.com"
- #define token @"CAES+wMIARKAAZhjH0EUOIhJMQBMjRywXq7MQ/cjLYg80Aho1ek0Jm63XMhr9Ocer8p1YaX1NTDiCFZWFkvlHf1pQhuxfKBc+mRR9KAbHUefqH+rdjZqjTF7p2m1wJXP8S6k+G2MpHrUe6TYBkJ43GhhTVFMuM3BZajY3VjZWOXBIODRIR1FKZjIiEjMzMzE0MjY0NzM5MTE4NjkxMSoLY2xpZGSSDgSDGAGESGTETqOio6c2RrLWRlbW8vKgoUYWNzOm9zczoqOio6c2RrLWRlbW9KEDExNDg5MzAxMDcyNDY4MThSBTI2ODQyWg9Bc3N1bWVkUm9sZVVzZXJgAGoSMzMzMTQyNjQ3MzkxMTg2OTExcglzZGstZGVtbzI="
-
- @implementation OssViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- self.view.backgroundColor = [UIColor whiteColor];
-
- }
-
- - (void)initAndUploadImage:(NSArray *)imageNames imageBase64Datas:(NSArray *)imageBase64Datas {
- [self.imageNames addObjectsFromArray:imageNames];
- [self.imageBase64Datas addObjectsFromArray:_imageBase64Datas];
-
- [self benginUploadTask];
- }
-
- - (void)benginUploadTask {
- NSOperationQueue * queue = [[NSOperationQueue alloc] init];
-
- NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
- //NSLog(@"%@", [NSThread currentThread]);
- }];
-
- for (NSInteger i = 0; i < 5; i ++) {
- [operation addExecutionBlock:^{
-
- NSData *imageData = [[NSData alloc] initWithBase64EncodedString:self.imageBase64Datas[i] options:0];
- [self uploadWithImageName:self.imageNames[i] imageData:imageData];
- }];
- }
-
- [operation setCompletionBlock:^{
- NSLog(@"all ok");
- }];
-
- [queue addOperation:operation];
- }
-
- - (void)uploadWithImageName:(NSString *)imageName imageData:(NSData *)imageData{
-
- OSSPutObjectRequest * put = [OSSPutObjectRequest new];
- put.bucketName = @"links123-images";
- put.objectKey = imageName;
-
- put.uploadingData = imageData; // 直接上传NSData
- put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
- NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
- };
-
- //https://links123-images.oss-cn-hangzhou.aliyuncs.com/test.png
- OSSTask * putTask = [self.client putObject:put];
- [putTask waitUntilFinished];
-
- if (!putTask.error) {
- NSLog(@"okay");
- }else {
- NSLog(@"failed");
- }
- }
-
- - (OSSClient *)client {
- if (!_client) {
- id<OSSCredentialProvider> provider = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:akid secretKeyId:skid securityToken:@""];
- _client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:provider];
- }
-
- return _client;
- }
-
- - (NSMutableArray *)imageNames {
- if (_imageNames) {
- _imageNames = [[NSMutableArray alloc] init];
- }
-
- return _imageNames;
- }
-
- - (NSMutableArray *)imageBase64Datas {
- if (_imageBase64Datas) {
- _imageBase64Datas = [[NSMutableArray alloc] init];
- }
-
- return _imageBase64Datas;
- }
-
- @end
|