Ali cloud HTTP (for reference only)

OSS Ali cloud http

Initialize the OSS / / token

#pragma mark – Ali cloud client OSS client STS temporary token

– (void) initOSSClient{

Uc_authcode *ucCode = [[uc_authcode alloc]init];

NSString *encryptionString = [ucCode getEncryptionString];

NSDictionary *callParamsDic = alloc]initWithObjectsAndKeys:@ “ossStsbyram”, @ “action”, nil] [[NSDictionary;

[AFNetWorkTool post:NEW_USERADDRESS encryptionString:encryptionString params:callParamsDic success:^ (ID responseObj) {

GJLog (@ “% @”, responseObj);

Int nStautus = [[responseObj objectForKey:@ “status” intValue];

NSString *contentStr = [responseObj objectForKey:@ “content”];

If (nStautus = = 1) {

NSDictionary *dic = [responseObj objectForKey:@ “data”];

_accessKeyId = [dic objectForKey:@ “AccessKeyId”];

_accessKeySecret =[dic objectForKey:@ “AccessKeySecret”];

_expiration =[dic objectForKey:@ “Expiration”];

_securityToken = [dic objectForKey:@ “SecurityToken”];

} else{

[self.view makeToast:contentStr];

}

} failure:^ (NSError *error) {

}];

}

Set / / Ali cloud token

[self xys_ALIYUNSend];

#pragma mark – aliyun token set

– (void) xys_ALIYUNSend{

ID credential = [[OSSFederationCredentialProvider initWithFederationTokenGetter:^OSSFederationToken * {alloc]

OSSFederationToken * token = [OSSFederationToken new];

Token.tAccessKey = _accessKeyId;

Token.tSecretKey = _accessKeySecret;

Token.tToken = _securityToken;

Token.expirationTimeInGMTFormat = _expiration;

Return token;

}];

OSSClientConfiguration * conf = [OSSClientConfiguration new];

Conf.maxRetryCount = 2;

Conf.timeoutIntervalForRequest = 30;

Conf.timeoutIntervalForResource = 24 * 60 * 60;

Client = [[OSSClient initWithEndpoint:endPoint credentialProvider:credential alloc] clientConfiguration:conf];

}

Ali cloud http / /

[self xys_ALIYUNsendVideo];

#pragma mark – http

– (void) xys_ALIYUNsendVideo{

[self.view addSubview:self.videoHud];

[SingleManager shareManager].isResumableUpload = YES;

Ali cloud video upload / method

__block NSString * recordKey;

NSURL *filePath = [SingleManager shareManager].videoURL;

NSString * bucketName = @ xys-app-file”;

NSString * objectKey;

If (_uploadid = = Nil) {

ObjectKey = [self getObjectKey];

_uploadid = objectKey;

} else{

ObjectKey = _uploadid;

}

[[[[[[OSSTask taskWithResult:nil] continueWithBlock:^id (OSSTask *task) {

The file structure / / a unique record key

/ NSURL * fileURL = [NSURL fileURLWithPath:filePath];

NSDate * lastModified;

NSError * error;

[filePath getResourceValue:& lastModified forKey:NSURLContentModificationDateKey error:& error];

If (error) {

Return [OSSTask taskWithError:error];

}

RecordKey = “[NSString stringWithFormat:@%@-%@-%@-%@”, bucketName, objectKey, [OSSUtil getRelativePath:[filePath absoluteString]], lastModified];

NSLog (@ recordKeyrecordKeyrecordKey——-%@), recordKey);

/ / view local whether to save the unfinished UploadId by recording key

NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];

Return [OSSTask taskWithResult:[userDefault objectForKey:recordKey]];

}]

ContinueWithSuccessBlock:^id (OSSTask *task) {

If ((task.result)) {

If there is no local records / call, initialize the UploadId interface to obtain

OSSInitMultipartUploadRequest * initMultipart = [OSSInitMultipartUploadRequest new];

InitMultipart.bucketName = bucketName;

InitMultipart.objectKey = objectKey;

InitMultipart.contentType = @ application/octet-stream”;

Return [client multipartUploadInit:initMultipart];

}

OSSLogVerbose (“An resumable task for uploadid: @% @”, task.result);

Return task;

}]

ContinueWithSuccessBlock:^id (OSSTask *task) {

NSString * uploadId = nil;

If (task.error) {

Return task;

}

If ([task.result isKindOfClass:[OSSInitMultipartUploadResult class]]) {

UploadId = ((OSSInitMultipartUploadResult *) task.result).UploadId;

} else {

UploadId = task.result;

}

If ((uploadId)) {

Return [OSSTask taskWithError:[NSError errorWithDomain:OSSClientErrorDomain

Code:OSSClientErrorCodeNilUploadid

UserInfo:@{OSSErrorMessageTOKEN: @ Can’t get an upload ID “}]];

}

“Record key: UploadId / /” persistence to the local storage

NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];

[userDefault setObject:uploadId forKey:recordKey];

[userDefault synchronize];

Return [OSSTask taskWithResult:uploadId];

}]

ContinueWithSuccessBlock:^id (OSSTask *task) {

Hold / / UploadId upload files

OSSResumableUploadRequest * resumableUpload = [OSSResumableUploadRequest new];

ResumableUpload.bucketName = bucketName;

ResumableUpload.objectKey = objectKey;

ResumableUpload.uploadId = task.result;

ResumableUpload.uploadingFileURL = filePath;

ResumableUpload.uploadProgress = ^ (int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {

Float number = (float) totalBytesSent/ (float) totalBytesExpectedToSend;

Self.videoHud.progress = number;

};

Return [client resumableUpload:resumableUpload];

}]

ContinueWithBlock:^id (OSSTask *task) {

If (task.error) {

If ([task.error.domain isEqualToString:OSSClientErrorDomain] & & task.error.code = = OSSClientErrorCodeCannotResumeUpload) {

Http / / if failed and cannot be recovered, need to delete the local records of the UploadId, and then restart the task

[[NSUserDefaults standardUserDefaults] removeObjectForKey:recordKey];

}

} else {

Dispatch_async (dispatch_get_main_queue), (^{

[self.videoHud hide:YES];

Self.videoHud = nil;

/ / delete sandbox below correspond to video files

/ NSFileManager *defauleManager = [NSFileManager defaultManager];

/ NSString *tempPath = [filePath absoluteString];

/ [defauleManager removeItemAtPath:tempPath error:nil];

NSFileManager *fileManager = [NSFileManager defaultManager];

NSArray *fileListArray = [fileManager contentsOfDirectoryAtPath:[NSHomeDirectory () stringByAppendingPathComponent:@ “TMP”] error:nil];

For (NSString *file in fileListArray)

{

NSString *path = [[NSHomeDirectory () stringByAppendingPathComponent:@ “TMP”] stringByAppendingPathComponent:file];

NSString *extension = [path pathExtension];

If ([extension compare:@ mp4 options:NSCaseInsensitiveSearch] = = NSOrderedSame)

{

[fileManager removeItemAtPath:path error:nil];

}

}

});

NSLog (@ upload complete!);

/ / upload success, delete the local save UploadId

_uploadid = nil;

[[NSUserDefaults standardUserDefaults] removeObjectForKey:recordKey];

[self uploadVideoInfoWithObjectKey:objectKey stringWithFormat:@ “%f.1”, VideoDuration]] duration:[NSString;

}

Return nil;

}];

}

OSS Ali cloud http

[self uploadVideoInfoWithObjectKey:objectKey duration:[NSString stringWithFormat:@”%f.1″,VideoDuration]];

}

return nil;

}];

}