开发者社区 问答 正文

上传文件图片报如下错误

代码如下:
- (void)uploadObjectAsync:(NSData *)objectData objectKey:(NSString *)objectKey{
    id<OSSCredentialProvider> credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:AccessKey secretKey:AccessSecretKey];
    OSSClientConfiguration * conf = [OSSClientConfiguration new];
    conf.maxRetryCount = 2;
    conf.timeoutIntervalForRequest = 30;
    conf.timeoutIntervalForResource = 24 * 60 * 60;
    client = [[OSSClient alloc] initWithEndpoint:endPoint credentialProvider:credential clientConfiguration:conf];

    OSSPutObjectRequest * put = [OSSPutObjectRequest new];
    put.bucketName = bucketName;
    put.objectKey = objectKey;
    put.uploadingData = objectData; // 直接上传NSData
    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=hschjt-test.oss-cn-hangzhou.aliyuncs.com, Bucket=hschjt-test, Endpoint=oss-cn-beijing.aliyuncs.com, Code=AccessDenied, RequestId=58660589CDF47451110B5578, Message=The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.}

展开
收起
李景199409 2016-12-30 15:29:36 3269 分享
分享
版权
举报
阿里云 AI 助理回答

从错误信息来看,您在尝试上传文件到阿里云OSS(对象存储服务)时遇到了AccessDenied错误,并提示您需要使用指定的Endpoint。具体来说,错误信息指出您应该将所有未来的请求发送到这个特定的Endpoint:hschjt-test.oss-cn-hangzhou.aliyuncs.com

问题在于您的代码中设置的Endpoint与Bucket所在的区域不匹配。根据错误信息,您的Bucket位于杭州区域(oss-cn-hangzhou),但您在配置客户端时使用的Endpoint是北京区域(oss-cn-beijing.aliyuncs.com)。为了解决这个问题,您需要将客户端初始化中的Endpoint更改为与Bucket位置相匹配的Endpoint。

修改代码如下:

// 将Endpoint更改为正确的区域Endpoint
client = [[OSSClient alloc] initWithEndpoint:@"oss-cn-hangzhou.aliyuncs.com" credentialProvider:credential clientConfiguration:conf];

这样修改后,您的客户端将会连接到正确的区域Endpoint,从而解决AccessDenied错误。请确保在实际应用中始终使用与您的Bucket所在区域相匹配的Endpoint。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等