鉴于移动云的文档写的奇烂无比,用移动云官方的sdk还不如用回亚马逊的sdk,直接正题。
使用步骤
第一步,添加依赖包文件:
dependencies {
compile'com.amazonaws:aws-android-sdk-s3:2.6.10'
}
第二步,添加需要的权限和配置文件:
<!-- 亚马逊云需要配置服务 -->
<service android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
android:enabled="true" />
<!-- 需要权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
第三步:配置亚马逊的参数等信息:
private static String hostname = "hostname";
private static String accessKey = "accessKey";
private static String secretKey = "secretKey";
private static String bucketName = "bucketName";
第四步:创建AmazonS3Client:
AmazonS3Client client = new AmazonS3Client(new AWSCredentials() {
@Override
public String getAWSAccessKeyId() {
return accessKey;
}
@Override
public String getAWSSecretKey() {
return secretKey;
}
});
第五步:给新创建的AmazonS3Client配置hostname(hostname要跟移动云的一致,不然不行):
client.setEndpoint(hostname);
第六步:上传文件(下面示范上传图片):
public boolean upDate(byte[] content,String key){
ObjectMetadata meta = new ObjectMetadata();
String objectKey = key;
meta.setContentType("image/jpeg");
PutObjectRequest request = new PutObjectRequest(bucketName,objectKey,new ByteArrayInputStream(content),meta);
request.setCannedAcl(CannedAccessControlList.Private);
PutObjectResult result= client.putObject(request);
if(result != null){
return true;
}return false;
}
版权声明:本文为qq_27871511原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。