本文介紹如何使用天翼雲對象存儲服務java
在註冊天翼雲帳號以後,進入控制檯,建立祕鑰,拿到AccessKeyID和SecretAccessKey用於訪問對象存儲API算法
在官網選擇對於的jar包spring
https://www.ctyun.cn/h5/help2/10000101/10001740
在pom.xml中加入如下配置apache
<dependency> <groupId>cn.ctyun</groupId> <artifactId>oos-sdk</artifactId> <version>6.5.0</version> <scope>system</scope> <type>jar</type> <systemPath>${project.basedir}/src/main/resources/lib/oos-sdk-6.5.0.jar</systemPath> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.10.3</version> </dependency>
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(); //設置鏈接的超時時間,單位毫秒 clientConfig.setConnectionTimeout(30 * 1000); //設置 socket 超時時間,單位毫秒 clientConfig.setSocketTimeout(30 * 1000); clientConfig.setProtocol(Protocol.HTTP); //設置 http //設置 V4 簽名算法中負載是否參與簽名,關於簽名部分請參看《OOS 開發者文檔》 S3ClientOptions options = new S3ClientOptions(); options.setPayloadSigningEnabled(true); // 建立 client AmazonS3 oosClient = new AmazonS3Client( new PropertiesCredentials(accessKey, secretKey), clientConfig); // 設置 endpoint oosClient.setEndpoint(endpoint); //設置選項 oosClient.setS3ClientOptions(options); return oosClient; } }
public void createBucket(String bucketName) { Bucket bucketInfo = ossClient.createBucket(bucketName,"ChengDu","ShenYang"); }
public void listBuckets() { List<Bucket> listBuckets = ossClient.listBuckets(); for (Bucket bucketInfo : listBuckets) { System.out.println("listBuckets:" + "\t Name:" + bucketInfo.getName() + "\t CreationDate:" + bucketInfo.getCreationDate()); } }
public 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(); }