整理一下關於阿里雲OSS API的相關實現
Tips:
1> 任何第三方工具的接入都要先找官方的幫助文檔
2> 把 Github 作為最好的代碼搜索工具
具體的實現:
1. 需要的maven依賴:
<dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.11.2</version> </dependency>
2. 需要的屬性, 相應的屬性需要配置在yaml中
@Value("${OSS.endpoint}")
private String endpoint;
@Value("${OSS.accessKeyId}")
private String accessKeyId;
@Value("${OSS.accessKeySecret}")
private String accessKeySecret;
@Value("${OSS.bucketName}")
private String bucketName;
yaml:
OSS:
endpoint: http://oss-cn-hangzhou.aliyuncs.com
accessKeyId: *************
accessKeySecret: ******************
bucketName: my-bucket
3.OSSClient對象的創建:
public OSSClient ossClient() { ClientConfiguration conf = new ClientConfiguration(); conf.setSupportCname(true); CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret); OSSClient ossClient = new OSSClient(endpoint, credentialsProvider, conf); return ossClient; }
官方文檔也提供了相應的實現示例,可以參考:
https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.931.726f7a74qDvLRY
4. 通過ossClient實現文件的上傳,下載,刪除等功能,官方的幫助文檔寫的非常詳細,直接上鏈接:
OSSClient幫助文檔:
https://help.aliyun.com/product/31815.html?spm=a2c4g.11186623.6.540.296f5338D4UArU
Java SDK的多種文件上傳方式:
https://help.aliyun.com/document_detail/32013.html?spm=a2c4g.11186623.2.12.172e62e7dDPtWm
Java SDK的文件下載方式:
https://help.aliyun.com/document_detail/32014.html?spm=a2c4g.11186623.6.961.64c26d138zC2qB
