天翼云对象存储android实现,使用天翼云对象存储服务
使用天翼云對象存儲服務(wù)
本文介紹如何使用天翼云對象存儲服務(wù)
安全憑證
在注冊天翼云賬號之后,進(jìn)入控制臺,創(chuàng)建秘鑰,拿到AccessKeyID和SecretAccessKey用于訪問對象存儲API
下載SDK
在官網(wǎng)選擇對于的jar包https://www.ctyun.cn/h5/help2/10000101/10001740
POM文件配置
在pom.xml中加入以下配置
cn.ctyun
oos-sdk
6.5.0
system
jar
${project.basedir}/src/main/resources/lib/oos-sdk-6.5.0.jar
commons-httpclient
commons-httpclient
3.1
org.apache.httpcomponents
httpclient
4.5.2
joda-time
joda-time
2.10.3
OSS配置import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OOSConfig {
@Value("${OOS_ACCESS_KEY}")
private String accessKey;
@Value("${OOS_SECRET_KEY}")
private String secretKey;
@Value("${OOS_ENDPOINT}")
private String endpoint;
@Bean
public AmazonS3 oosClient() {
ClientConfiguration clientConfig = new ClientConfiguration();
//設(shè)置連接的超時(shí)時(shí)間,單位毫秒
clientConfig.setConnectionTimeout(30 * 1000);
//設(shè)置 socket 超時(shí)時(shí)間,單位毫秒
clientConfig.setSocketTimeout(30 * 1000);
clientConfig.setProtocol(Protocol.HTTP); //設(shè)置 http
//設(shè)置 V4 簽名算法中負(fù)載是否參與簽名,關(guān)于簽名部分請參看《OOS 開發(fā)者文檔》
S3ClientOptions options = new S3ClientOptions();
options.setPayloadSigningEnabled(true);
// 創(chuàng)建 client
AmazonS3 oosClient = new AmazonS3Client(
new PropertiesCredentials(accessKey,
secretKey), clientConfig);
// 設(shè)置 endpoint
oosClient.setEndpoint(endpoint);
//設(shè)置選項(xiàng)
oosClient.setS3ClientOptions(options);
return oosClient;
}
}
創(chuàng)建Bucketpublic void createBucket(String bucketName) {
Bucket bucketInfo = ossClient.createBucket(bucketName,"ChengDu","ShenYang");
}
查詢Bucketpublic void listBuckets() {
List listBuckets = ossClient.listBuckets();
for (Bucket bucketInfo : listBuckets) {
System.out.println("listBuckets:"
+ "\t Name:" + bucketInfo.getName()
+ "\t CreationDate:" + bucketInfo.getCreationDate());
}
}
刪除Bucketpublic void deleteBucket(String bucketName) {
ossClient.deleteBucket(bucketName);
}
上傳文件public String uploadFile(InputStream inputStream, String fileName) {
String key = generateKey(fileName);
PutObjectRequest request = new PutObjectRequest(bucketName, key, inputStream, null);
request.setStorageClass(StorageClass.ReducedRedundancy);
PutObjectResult result = ossClient.putObject(request);
URL url = ossClient.generatePresignedUrl(new GeneratePresignedUrlRequest(bucketName, key));
return String.valueOf(url);
}
刪除文件public void deleteFile(String fileKey) {
ossClient.deleteObject(bucketName, fileKey);
}
獲取文件下載地址public String getDownloadUrl(String fileKey) {
GeneratePresignedUrlRequest request = new
GeneratePresignedUrlRequest(bucketName, fileKey);
URL url = ossClient.generatePresignedUrl(request);
return url.toString();
}
總結(jié)
以上是生活随笔為你收集整理的天翼云对象存储android实现,使用天翼云对象存储服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装gitlab遇到的大坑!!!
- 下一篇: i5配置的计算机主机功耗,电脑配置电源多