IOS 使用FederationToken上传文件, 未得到 tokenSTS 上传文件却不报错
ReIOS 使用FederationToken上传文件, 未得到 tokenSTS 上传文件却不报错
credentialProvider 构造代码:
+(void)initOSSClient {
id credential2 = [[OSSFederationCredentialProvider alloc] initWithFederationTokenGetter:^OSSFederationToken * {
NSURL * url = [NSURL URLWithString:@'http://192.168.0.100:8080/xxx/];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
OSSTaskCompletionSource * tcs = [OSSTaskCompletionSource taskCompletionSource];
NSURLSession * session = [NSURLSession sharedSession];
NSURLSessionTask * sessionTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
[tcs setError:error];
return;
}
[tcs setResult:data];
}];
[sessionTask resume];
[tcs.task waitUntilFinished];
if (tcs.task.error) {
NSLog(@'get token error: %@', tcs.task.error);
return nil;
} else {
NSDictionary * object = [NSJSONSerialization JSONObjectWithData:tcs.task.result
options:kNilOptions
error:nil];
OSSFederationToken * token = [OSSFederationToken new];
NSDictionary *tempobject=[object objectForKey:@'Federation'];
token.tAccessKey = [tempobject objectForKey:@'accesskeyid'];
token.tSecretKey = [tempobject objectForKey:@'accesskeysecret'];
token.tToken = [tempobject objectForKey:@'securitytoken'];
token.expirationTimeInGMTFormat = [tempobject objectForKey:@'expiration'];
NSLog(@'get token: %@', token);
//2015-11-03T08:51:05Z
return token;
}
}];
OSSClientConfiguration * conf = [OSSClientConfiguration new];
conf.maxRetryCount = 2;
conf.timeoutIntervalForRequest = 30;
conf.timeoutIntervalForResource = 24 * 60 * 60;
client = [[OSSClient alloc] initWithEndpoint:endPoint credentialProvider:credential2 clientConfiguration:conf];
}
-------------------------
ReIOS 使用FederationToken上传文件, 未得到 tokenSTS 上传文件却不报错
// 同步批量上传代码
+ (void)uploadBatchSyncByname: (NSString*) bucketname :(NSMutableArray *) uploadarray : (int) pictype :(NSString*)todir uploadstatusblock:(void(^) (int ))handle;
{
NSString *tempdir;
tempdir=NSTemporaryDirectory();
int uploadresult; //上传状态 0:有文件上传失败 1:上传成功
uploadresult=1;
if (!client )
{
[OSSTools initOSSClient];
}
//循环上传文件
for (int i=0; i {
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
// required fields
put.bucketName = bucketname;
put.objectKey = [uploadarray objectAtIndex:i];
NSString *photoname=[put.objectKey lastPathComponent];
put.uploadingFileURL = [NSURL fileURLWithPath:[tempdir stringByAppendingPathComponent:photoname]];
// optional fields
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
NSLog(@'%lld, %lld, %lld', bytesSent, totalByteSent, totalBytesExpectedToSend);
};
put.contentType = @'';
put.contentMd5 = @'';
put.contentEncoding = @'';
put.contentDisposition = @'';
OSSTask * putTask = [client putObject:put];
[putTask waitUntilFinished]; // 阻塞直到上传完成
if (!putTask.error) {
NSLog(@'upload 文件 %d success!',i);
//上传成功,将文件
NSString *sourcepath=[ tempdir stringByAppendingPathComponent:photoname] ;
NSString *topath=[todir stringByAppendingPathComponent:photoname];
[FileTools copyFile:sourcepath toPath: topath];
} else {
NSLog(@'upload 文件 %d failed, error: %@' ,i, putTask.error);
uploadresult=0;
break;
}
}
//回调返回上传状态
handle(uploadresult);
}
-------------------------
ReIOS 使用FederationToken上传文件, 未得到 tokenSTS 上传文件却不报错
1.看这log ,就是无法得到 token报错 :get token error:
2. 但是上传时却显示成功,因为 task.error为空
if (!putTask.error) {
NSLog(@'upload 文件 %d success!',i);
//上传成功,将文件
2016-01-14 17:48:55.363 ReleaseLife[1813:776631]
get token error: Error Domain=NSURLErrorDomain Code=-1001 '请求超时。' UserInfo={NSUnderlyingError=0x15648fd30 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 '(null)' UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://192.168.0.100:8080/xxx, NSErrorFailingURLKey=http://192.168.0.100:8080/xxx, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=请求超时。}
2016-01-14 17:49:24.550 ReleaseLife[1813:776149] upload 文件 0 success!
赞0
踩0