aloestec-rn-oss
v1.1.0
Published
A cross-platform React Native SDK for AliyunOSS Services, providing most of the OSS APIs, like client initialization, uploading & downloading etc.
Downloads
4
Readme
阿里云OSS
安装
yarn add aloestec-rn-oss
自动引入
react-native link aloestec-rn-oss
手动引入
Android
- 打开文件
android/app/src/main/java/[...]/MainApplication.java
- 引入
import com.aloestec.oss.RNAliyunOssPackage
- 在
getPackages()
方法中添加new RNAliyunOssPackage()
- 打开文件
android/settings.gradle
并添加:
include ':aloestec-rn-oss'
project(':aloestec-rn-oss').projectDir = new File(rootProject.projectDir, '../node_modules/aloestec-rn-oss/android')
- 打开文件
android/app/build.gradle
并添加:
dependencies{
...
compile project(':aloestec-rn-oss')
}
IOS
在Xcode中选择 Libraries ➜ Add Files to [your project's name]。 选择 node_modules ➜ aloestec-rn-oss and add RNAliyunOss.xcodeproj
在XCode中, 到 Build Phases ➜ Link Binary With Libraries 添加 libRNAliyunOSS.a
在Xcode中选择 Frameworks ➜ Add Files to [your project's name]. 选择 node_modules ➜ aloestec-rn-oss ➜ AliyunSDK 添加 AliyunOSSiOS.framework
如果报错:framework not find AliyunOSSiOS , 设置 Build Settings -> Search Paths -> Framework Search Paths ,添加AliyunSDK的路径
issue
Undefined symbols for architecture arm64:
"_res_9_getservers", referenced from:
-[OSSIPv6Adapter getDNSServersIpStack] in AliyunOSSiOS(OSSIPv6Adapter.o)
"_res_9_ninit", referenced from:
-[OSSIPv6Adapter getDNSServersIpStack] in AliyunOSSiOS(OSSIPv6Adapter.o)
"_res_9_ndestroy", referenced from:
-[OSSIPv6Adapter getDNSServersIpStack] in AliyunOSSiOS(OSSIPv6Adapter.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
原因是不兼容IPv6-Only网路
解决方法:
OSS移动端SDK为了解决无线网络下域名解析容易遭到劫持的问题,已经引入了HTTPDNS进行域名解析, 直接使用IP请求OSS服务端。在IPv6-Only的网络下,可能会遇到兼容性问题。 而APP官方近期发布了关于IPv6-only网络环境兼容的APP审核要求,为此,SDK从2.5.0版本开始已经做了兼容性处理。 在新版本中,除了-ObjC的设置,还需要引入两个系统库:
libresolv.tbd
SystemConfiguration.framework
CoreTelephony.framework
项目默认不支持data://
的格式上传,需要改源码
安卓:
@ReactMethod
public void asyncUpload(String bucketName, String ossFile, String sourceFile, final Promise promise) {
// 构造上传请求
if (sourceFile != null) {
sourceFile = sourceFile.replace("file://", "");
}
PutObjectRequest put = null;
if(sourceFile.startsWith("data://")) put = new PutObjectRequest(bucketName, ossFile, sourceFile.substring(7).getBytes(UTF8));
else put = new PutObjectRequest(bucketName, ossFile, sourceFile);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("application/octet-stream");
put.setMetadata(metadata);
...
IOS:
-(void)beginUploadingWithFilepath:(NSString *)filepath resultBlock:(void (^) (NSData *))callback {
// read asset data from filepath
if ([filepath hasPrefix:@"data://"]) {
NSData* data = [[filepath substringFromIndex:7] dataUsingEncoding:NSUTF8StringEncoding];
callback(data);
} else if ([filepath hasPrefix:@"assets-library://"]) {
PHAsset *asset = [PHAsset fetchAssetsWithALAssetURLs:@[filepath] options:nil].firstObject;
[self convertToNSDataFromAsset:asset withHandler:callback];
} else if ([filepath hasPrefix:@"localIdentifier://"]) {
NSString *localIdentifier = [filepath stringByReplacingOccurrencesOfString:@"localIdentifier://" withString:@""];
PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:nil].firstObject;
[self convertToNSDataFromAsset:asset withHandler:callback];
} else {
NSData *data = [NSData dataWithContentsOfFile:filepath];
callback(data);
}
}