开发者社区> 问答> 正文

iOS上传图片

vitabeetommy 2016-03-17 11:16:42 3161

直接粘代码
OSSClient *client;

        NSString *endpoint = [NSString stringWithFormat:@"http://%@",token.host_name];
        //注意在控制台获取域名之后,要在前面加上http://,并去掉bucketName字段。例如从控制台获取域名为“<bucketName>.oss-cn-beijing.aliyuncs.com”,则hostId应为“http://oss-cn-beijing.aliyuncs.com
        id<OSSCredentialProvider> credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:token.key_id secretKey:token.key_secret];
        
        client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];
        //初始化client
        
        OSSPutObjectRequest * put = [OSSPutObjectRequest new];
        
        // 必填字段
        put.bucketName = token.host_name;
        put.objectKey = @"packagePass";
        UIImage *image = [UIImage imageNamed:@"packagePass"];
        
        
        //    put.uploadingFileURL = [NSURL fileURLWithPath:@"<filepath>"];
        put.uploadingData =  UIImagePNGRepresentation(image); // 直接上传NSData

        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=vitabeedev.oss-cn-hangzhou.aliyuncs.com, Message=The OSS Access Key Id you provided does not exist in our records., OSSAccessKeyId=STS.sMk9KLTRyRhV7qsQ6hkn, Code=InvalidAccessKeyId, RequestId=56EA1CE305200A833146CABF}

对象存储 iOS开发
分享到
取消 提交回答
全部回答(0)
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

推荐文章
相似问题