开发者社区> 问答> 正文

iOS SDK报Token没被赋值

RCT_EXPORT_METHOD(initWithKey:(NSString *)AccessKey
                  SecretKey:(NSString *)SecretKey
                  securityToken:(NSString *)SecretToken
                  Endpoint:(NSString *)Endpoint){
  
    id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:AccessKey secretKeyId:SecretKey securityToken:SecretToken];
    
    OSSClientConfiguration * conf = [OSSClientConfiguration new];
    conf.maxRetryCount = 3; // 网络请求遇到异常失败后的重试次数
    conf.timeoutIntervalForRequest = 30; // 网络请求的超时时间
    conf.timeoutIntervalForResource = 24 * 60 * 60; // 允许资源传输的最长时间
    NSString *endpoint = Endpoint;
    
    client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential clientConfiguration:conf];


}
RCT_REMAP_METHOD(uploadObjectAsync, bucketName:(NSString *)BucketName
                 SourceFile:(NSString *)SourceFile
                 OssFile:(NSString *)OssFile
                 resolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject) {
  
  OSSPutObjectRequest * put = [OSSPutObjectRequest new];
  
  put.bucketName = BucketName;
  put.objectKey = OssFile;
  put.uploadingFileURL = [NSURL fileURLWithPath:SourceFile];
  NSLog(@"uploadingFileURL: %@", put.uploadingFileURL);
  // optional fields
  put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
    NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
    [self sendEventWithName: @"uploadProgress" body:@{@"everySentSize":[NSString stringWithFormat:@"%lld",bytesSent],
                                                      @"currentSize": [NSString stringWithFormat:@"%lld",totalByteSent],
                                                      @"totalSize": [NSString stringWithFormat:@"%lld",totalBytesExpectedToSend]}];
    
  };
//  put.objectMeta = [NSMutableDictionary dictionaryWithObjectsAndKeys: UpdateDate, @"Date", nil];
  
  OSSTask * putTask = [client putObject:put];
  [putTask waitUntilFinished]; // 阻塞直到上传完成
  [putTask continueWithBlock:^id(OSSTask *task) {
    NSLog(@"objectKey: %@", put.objectKey);
    if (!task.error) {
      NSLog(@"upload object success!");
      resolve(@YES);
    } else {
      NSLog(@"upload object failed, error: %@" , task.error);
      reject(@"-1", @"not respond this method", nil);
    }
    return nil;
  }];

}



_uploadWithOSS(){

         AliyunOSS. enableOSSLog();
         const config = {
             AccessKey: this. state. dataJson. sts. Credentials. AccessKeyId,   // your accessKeyId
             SecretKey: this. state. dataJson. sts. Credentials. AccessKeySecret, // your accessKeySecret
             SecretToken: this. state. dataJson. sts. Credentials. SecurityToken, // your securityToken
          };
             const endPoint = 'https://oss-cn-beijing.aliyuncs.com'; // your endPoint
             // 初始化阿里云组件
             console. log( '1+1========');
             console. log( config);
             console. log( "1+1========");
             AliyunOSS. initWithKey( config. AccessKey, config. SecretKey, config. SecretToken, endPoint);

            
         var photoArr = [];
         for ( var i = 0, count = this. state. photos. length; i < count; i++) {
             photoArr. push( this. state. photos[ i][ "uri"]);
        }


         for( var j= 0; j < photoArr. length ; j++){

             const uploadConfig = {
                 bucketName: this. state. dataJson. oss. bucket,   //your bucketName
                 sourceFile: photoArr[ j], // local file path
                 ossFile: CONFIG. getConfig(). UserID,   // the file path uploaded to oss
              };
               console. log( uploadConfig. sourceFile);
               // 执行上传
               AliyunOSS. uploadObjectAsync( uploadConfig). then(( resp) => {
                   console. log( resp);
                 // 此处可以执行回调
              }). catch(( err) =>{
                 console. log( err);
                 // 执行失败回调
          });
        }
    }


所有有关OSS的信息都是后台传过来的。
错误提示:

upload object failed, error: Error Domain=com.aliyun.oss.clientError Code=9 "(null)" UserInfo={ErrorMessage=Catch exception - *** -[__NSDictionaryM setObject:forKey:]: object cannot be nil (key: x-oss-security-token)}

展开
收起
星游学院 2018-05-29 18:18:57 3149 0
1 条回答
写回答
取消 提交回答
  • ReiOS SDK报Token没被赋值
           同样的问题  楼主怎么解决的
    2018-08-03 15:17:18
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
聚划算iOS平台动态化方案LuaView 立即下载
滴滴出行iOS端瘦身实践 立即下载
一个跨平台的云服务SDK需要什么 立即下载