RCT_EXPORT_METHOD(initWithKey:(NSString *)AccessKey
SecretKey:(NSString *)SecretKey
securityToken:(NSString *)SecretToken
Endpoint:(NSString *)Endpoint){
id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:AccessKey secretKeyId:SecretKey securityToken:SecretToken];
OSSClientConfiguration * conf = [OSSClientConfiguration new];
conf.maxRetryCount = 3; // 网络请求遇到异常失败后的重试次数
conf.timeoutIntervalForRequest = 30; // 网络请求的超时时间
conf.timeoutIntervalForResource = 24 * 60 * 60; // 允许资源传输的最长时间
NSString *endpoint = Endpoint;
client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential clientConfiguration:conf];
}
RCT_REMAP_METHOD(uploadObjectAsync, bucketName:(NSString *)BucketName
SourceFile:(NSString *)SourceFile
OssFile:(NSString *)OssFile
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
put.bucketName = BucketName;
put.objectKey = OssFile;
put.uploadingFileURL = [NSURL fileURLWithPath:SourceFile];
NSLog(@"uploadingFileURL: %@", put.uploadingFileURL);
// optional fields
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
[self sendEventWithName: @"uploadProgress" body:@{@"everySentSize":[NSString stringWithFormat:@"%lld",bytesSent],
@"currentSize": [NSString stringWithFormat:@"%lld",totalByteSent],
@"totalSize": [NSString stringWithFormat:@"%lld",totalBytesExpectedToSend]}];
};
// put.objectMeta = [NSMutableDictionary dictionaryWithObjectsAndKeys: UpdateDate, @"Date", nil];
OSSTask * putTask = [client putObject:put];
[putTask waitUntilFinished]; // 阻塞直到上传完成
[putTask continueWithBlock:^id(OSSTask *task) {
NSLog(@"objectKey: %@", put.objectKey);
if (!task.error) {
NSLog(@"upload object success!");
resolve(@YES);
} else {
NSLog(@"upload object failed, error: %@" , task.error);
reject(@"-1", @"not respond this method", nil);
}
return nil;
}];
}
_uploadWithOSS(){
AliyunOSS.
enableOSSLog();
const
config = {
AccessKey:
this.
state.
dataJson.
sts.
Credentials.
AccessKeyId,
// your accessKeyId
SecretKey:
this.
state.
dataJson.
sts.
Credentials.
AccessKeySecret,
// your accessKeySecret
SecretToken:
this.
state.
dataJson.
sts.
Credentials.
SecurityToken,
// your securityToken
};
const
endPoint =
'https://oss-cn-beijing.aliyuncs.com';
// your endPoint
// 初始化阿里云组件
console.
log(
'1+1========');
console.
log(
config);
console.
log(
"1+1========");
AliyunOSS.
initWithKey(
config.
AccessKey,
config.
SecretKey,
config.
SecretToken,
endPoint);
var
photoArr = [];
for (
var
i =
0,
count =
this.
state.
photos.
length;
i <
count;
i++) {
photoArr.
push(
this.
state.
photos[
i][
"uri"]);
}
for(
var
j=
0;
j <
photoArr.
length ;
j++){
const
uploadConfig = {
bucketName:
this.
state.
dataJson.
oss.
bucket,
//your bucketName
sourceFile:
photoArr[
j],
// local file path
ossFile:
CONFIG.
getConfig().
UserID,
// the file path uploaded to oss
};
console.
log(
uploadConfig.
sourceFile);
// 执行上传
AliyunOSS.
uploadObjectAsync(
uploadConfig).
then((
resp)
=> {
console.
log(
resp);
// 此处可以执行回调
}).
catch((
err)
=>{
console.
log(
err);
// 执行失败回调
});
}
}
所有有关OSS的信息都是后台传过来的。
错误提示:
upload object failed, error: Error Domain=com.aliyun.oss.clientError Code=9 "(null)" UserInfo={ErrorMessage=Catch exception - *** -[__NSDictionaryM setObject:forKey:]: object cannot be nil (key: x-oss-security-token)}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。