开发者社区> 问答> 正文

第一次用oss上传不上图片,求救,急!!!!


NSDictionary * alidict1 = [aliDict objectForKey:@"Credentials"];
        NSString * accessKeyId = [alidict1 objectForKey:@"AccessKeyId"];
        NSString * accessKeySecret = [alidict1 objectForKey:@"AccessKeySecret"];
        NSLog(@"%@,%@",accessKeyId,accessKeySecret);
        NSString *endpoint = ALIYUN_END_POINT;
        id<OSSCredentialProvider> credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:accessKeyId secretKey:accessKeySecret];
        OSSClient * client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];
        OSSPutObjectRequest * put = [OSSPutObjectRequest new];
        put.bucketName = ALIYUN_BUCKET;


        put.objectKey = fileName;
        
        NSString * docDir = [self getDocumentDirectory];
        put.uploadingFileURL = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:fileName]];
        
//        NSString * userid = [users objectForKey:@"userid"];


//        put.uploadingFileURL = [NSURL fileURLWithPath:imageURL];
        NSLog(@"put.uploadingFileURL:%@",put.uploadingFileURL);
        put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
            // 当前上传段长度、当前已经上传总长度、一共需要上传的总长度
            NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
        };
        OSSTask * putTask = [client putObject:put];
        [putTask continueWithBlock:^id(OSSTask *task) {
            if (!task.error) {
                NSLog(@"upload object success!");
            }else{
                NSLog(@"upload object failed, error: %@",task.error);
            }
            return nil;
        }];
错误信息: 2015-12-16 16:57:52.470 HealthDoc[4130:1267782] upload object failed, error: Error Domain=com.aliyun.oss.clientError Code=6 "unsupported URL" UserInfo={OriginErrorCode=-1002, NSErrorFailingURLStringKey=(null)://(null)/IMG_0001.JPG, NSErrorFailingURLKey=(null)://(null)/IMG_0001.JPG, NSUnderlyingError=0x7fd1660bbd70 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSLocalizedDescription=unsupported URL}


我的endpoint、bucketName、objectKey都是有值得,是不是uploadingFileURL出错了

展开
收起
飞起的落叶 2015-12-16 17:34:25 15126 0
2 条回答
写回答
取消 提交回答
  • 回 1楼克劳德小弟的帖子
    解决了谢谢啊

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

    回 1楼克劳德小弟的帖子
    NSDictionary * alidict1 = [aliDict objectForKey:@"Credentials"];
            NSString * accessKeyId = [alidict1 objectForKey:@"AccessKeyId"];
            NSString * expiration = [alidict1 objectForKey:@"Expiration"];
            NSString * SecurityToken = [alidict1 objectForKey:@"SecurityToken"];
            NSString * accessKeySecret = [alidict1 objectForKey:@"AccessKeySecret"];
            OSSFederationToken * token = [OSSFederationToken new];
            token.tAccessKey = accessKeyId;
            token.tSecretKey = accessKeySecret;
            token.tToken = SecurityToken;
            token.expirationTimeInGMTFormat = expiration;
            NSLog(@"get token:%@",token);
            id<OSSCredentialProvider> credential = [[OSSFederationCredentialProvider alloc]initWithFederationTokenGetter:^OSSFederationToken *{
                return token;
            }];

            NSLog(@"%@,%@",accessKeyId,accessKeySecret);
            NSString *endpoint = ALIYUN_END_POINT;

            OSSClient * client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];
            OSSPutObjectRequest * put = [OSSPutObjectRequest new];
            put.bucketName = ALIYUN_BUCKET;

            put.objectKey = fileName;
            
            NSString * docDir = [self getDocumentDirectory];
            put.uploadingFileURL = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:fileName]];
            
            NSLog(@"put.uploadingFileURL:%@",put.uploadingFileURL);
            put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
                // 当前上传段长度、当前已经上传总长度、一共需要上传的总长度
                NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
            };
            OSSTask * putTask = [client putObject:put];
            [putTask continueWithBlock:^id(OSSTask *task) {
                if (!task.error) {
                    NSLog(@"upload object success!");
                }else{
                    NSLog(@"upload object failed, error: %@",task.error);
                }
                return nil;
            }];
    出现了一个新的问题,这样写完以后upload object failed, error: Error Domain=com.aliyun.oss.serverError Code=-403 "(null)" UserInfo={__name=Error, HostId=healthd-user-pic-stage.oss-cn-hangzhou.aliyuncs.com, Message=Access denied by authorizer's policy., Code=AccessDenied, RequestId=56721F398CDE4E0167DD6818}报这个错误,这是什么原因呢
    2015-12-16 18:52:28
    赞同 展开评论 打赏
  • 确认一下是否设置了endpoint,endpoint的格式如 http://oss-cn-hangzhou.aliyuncs.com 。
    2015-12-16 18:16:15
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载