开发者社区> 问答> 正文

IOS上传图片失败


    OSSClient *ossclient = [OSSClient sharedInstanceManage];
    [ossclient setGlobalDefaultBucketHostId:@"oss-cn-hangzhou.aliyuncs.com"];
    [ossclient setGlobalDefaultBucketAcl:PUBLIC_READ] ;
    
    [ossclient setGenerateToken:^NSString *(NSString *method, NSString *md5, NSString *type, NSString *date, NSString *xoss, NSString *resource){

        NSString *signature = nil;
        
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[[NSString stringWithFormat:@"http://%@/xxx/xxx?access_token=%@&appId=%@&httpMethod=%@&md5=%@&type=%@&date=%@&ossHeaders=%@&resource=%@",BASE_URL,K_ACCESSTOKEN,appId,method,md5,type,date,xoss,resource] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        
        [request setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    
        [request setHTTPMethod:@"GET"];
        
        // 发送同步请求, data就是返回的数据
        NSError *error = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
        if (data == nil) {
            NSLog(@"send request failed: %@", error);
            return nil;
        }
        
        NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"response is %@",response);
        
        signature=resource;
        
        return signature;

    }];
    
    OSSBucket *bucket = [[OSSBucket alloc] initWithBucket:@"deaas"];
    __block int success=0;
    for (int i=0; i<_comment.curTweet.imgs.count; i++) {

        OSSData *testData = [[OSSData alloc] initWithBucket:bucket withKey:[NSString stringWithFormat:@"%@.jpg",[Tool getUUID]]];
        [testData setData:UIImageJPEGRepresentation(_comment.curTweet.imgs , 1) withType:@"image/jpg"];

        [testData uploadWithUploadCallback:^(BOOL isSuccess, NSError *error) {
            if (isSuccess) {
                NSLog(@"success !!!!!!");
                success++;
            }
            else
            {
                NSLog(@"errorInfo_testDataUploadWithProgress:%@", [error userInfo]);
            }
        } withProgressCallback:^(float progress) {
          //  NSLog(@"current get %f", progress);
        }];
    }


返回错误是
{
    ArgumentName = Authorization;
    ArgumentValue = "/deaas/2843009C214B453FA9F99791A7FE7970.jpg";
    Code = InvalidArgument;
    HostId = "deaas.oss-cn-hangzhou.aliyuncs.com";
    Message = "Authorization header is invalid.";
    RequestId = 54FDC171957EEB7621C202FF;
}



展开
收起
ed1son 2015-03-10 00:27:41 14873 0
3 条回答
写回答
取消 提交回答
  • ReIOS上传图片失败
    解决没有?
    2015-07-29 17:31:26
    赞同 展开评论 打赏
  • ReIOS上传图片失败
    怎样自己实现signature???
    2015-04-15 19:19:28
    赞同 展开评论 打赏
  • 先使用下面这段代码替换掉你的加签函数,如果可以正常运行的话就是你的加签函数有问题,然后再仔细排查一下加签函数的编写逻辑~
    [ossclient setGenerateToken:^(NSString *method, NSString *md5, NSString *type, NSString *date, NSString *xoss, NSString *resource){
            NSString *signature = nil;
            NSString *content = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@%@", method, md5, type, date, xoss, resource];
            signature = [OSSTool calBase64Sha1WithData:content withKey:secretKey];
            signature = [NSString stringWithFormat:@"OSS %@:%@", accessKey, signature];
            return signature;
        }];
    2015-03-10 20:50:40
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

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