开发者社区> 问答> 正文

IOS 使用FederationToken上传文件, 未得到 tokenSTS 上传文件却不报错



我把获取 OSSFederationToken的服务端程序关掉了,debug程序时, 确认: credentialProvider的 cachedtoken为空,然后 初始化客户端

client = [[ OSSClient alloc ] initWithEndpoint : endPoint credentialProvider :credential2 clientConfiguration :conf];


再执行上传动作,按理说 没有token,而执行上传 [ client putObject :put];的话: putTask. error 应该报错啊,现在是上传不成功,但是putTask.error为空,不报错,所以程序认为上传已经成功了,这个获取不到FederationToken导致上传失败的情况怎么handle呢? 请教各位大神帮忙解答,谢谢!

  
//上传文件
        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]; // [font='Heiti SC Light']阻塞直到上传完成
        
         if (!putTask. error) {
          
             NSLog (@"upload [font='Heiti SC Light']文件 %d success!" ,i);
             // [font='Heiti SC Light']上传成功,将文件

         } else {
            
             NSLog (@"upload [font='Heiti SC Light']文件 %d failed, error: %@" ,i, putTask. error );
            uploadresult= 0;
             break;

展开
收起
佛缘书法 2016-01-14 15:46:55 16247 0
2 条回答
写回答
取消 提交回答
  • ReIOS 使用FederationToken上传文件, 未得到 tokenSTS 上传文件却不报错
    credentialProvider 构造代码:

    +(void)initOSSClient {
        
           id<OSSCredentialProvider> 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<uploadarray.count; 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!
    2016-01-14 17:22:39
    赞同 展开评论 打赏
  • credentialProvider的构造代码贴一下?

    如果里面返回token为nil的话,是会报错的:upload object failed, error: Error Domain=com.aliyun.oss.clientError Code=1 "(null)" UserInfo={ErrorMessage=Can't get a federation token}。

    -------------------------

    那问下,你说的 “获取OSSFederationToken的服务端程序关掉” 之后,确认[[OSSFederationCredentialProvider alloc] initWithFederationTokenGetter:^OSSFederationToken * {...} 的 OSSFederationToken Block一直返回nil吗?

    可以加一句话打印下。我这边确认如果block返回nil,是会报错的。

    -------------------------

    麻烦确认下sdk版本

    -------------------------

    建议提交工单吧,把能复现的demo提交给客服处理。或者,把能复现的demo打包上传,这里贴链接我去下载来试一下。

    2016-01-14 16:05:41
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载