开发者社区> 问答> 正文

iOS用OSS上传一张图片,不成功,怎么回事?

我尝试使用阿里云的OSS iOS版的,用demo上传一张图片,但是都不成功,以下是我的代码(图片源没问题):

NSString * endpoint = @"http://:oss-tests1.img-cn-shenzhen.aliyuncs.com/OBJ";   //我不清楚这个endpoint写错了么?域名我从控制器拿到两个内网外网的,都一样结果!!!

    id<OSSCredentialProvider> credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:@"AccessKey"
                                                                                                            secretKey:@"secretKey"];
    OSSClient * client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];
    
    
    OSSPutObjectRequest * put = [OSSPutObjectRequest new];
    

    NSData * data = UIImageJPEGRepresentation(_image, 1.0);
    
    put.bucketName = @"oss-tests1";
    put.objectKey = @"ty.png";     //这个objectKey是指什么?文件夹名字还是上传后的文件名字?
    put.uploadingData = data;
    
    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.clientError Code=6 "Could not connect to the server." UserInfo={NSErrorFailingURLKey=http://localhost:oss-tests1.img-cn-shenzhen.aliyuncs.com/OBJ/ty.png, _kCFStreamErrorDomainKey=1, OriginErrorCode=-1004, NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=http://localhost:oss-tests1.img-cn-shenzhen.aliyuncs.com/OBJ/ty.png, NSUnderlyingError=0x7f9da603cee0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _kCFStreamErrorCodeKey=61}

展开
收起
烈血残阳 2016-06-13 15:16:13 8914 0
3 条回答
写回答
取消 提交回答
  • 处理了吗?

    2020-08-08 21:25:47
    赞同 展开评论 打赏
  • 一个程序员,欢迎骚扰!!!
    看错误信息 Could not connect to the server. 无法连接服务 是不是那些配置参数没配置好
    2016-06-14 20:53:40
    赞同 展开评论 打赏
  • ReiOS用OSS上传一张图片,不成功,怎么回事?
    阿里团队的大神快来帮忙看看啊,来拯救一下本菜鸟!!!
    2016-06-13 15:38:15
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

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