DEMO GitLab
更多源碼請移步GitHub
https://github.com/xiiiblue/demo-oss
OSS SDK下載
https://promotion.aliyun.com/ntms/act/ossdoclist.html
OSS SDK源碼
https://github.com/aliyun/aliyun-oss-java-sdk
OSS Maven依賴
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.1</version>
</dependency>
OSS官方文檔
https://help.aliyun.com/document_detail/32009.html
申請及配置OSS流程
登錄aliyun后,進入OSS控制臺:
https://oss.console.aliyun.com/overview右側(cè)點擊"購買資源包"叫乌,選擇合適的檔位柴罐,付費購買。
右側(cè)點擊"新建Bucket"憨奸,輸入一個全局唯一的bucket名革屠,并選擇所屬地域,新建Bucket膀藐。
左側(cè)列表中屠阻,進入新建的Bucket,可以查詢到"Endpoint"
獲取AK额各,可以直接訪問鏈接:
https://ak-console.aliyun.com/#/accesskey
常用操作示例
private Logger logger = LoggerFactory.getLogger(OssComponent.class);
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Autowired
private OSSClient ossClient;
/**
* 通過bucket與fileKey獲取URL
*
* @param bucket
* @param fileKey
* @return
*/
public String urlFromFileKey(String bucket, String fileKey) {
return "http://" + bucket + "." + this.endpoint + "/" + fileKey;
}
/**
* 上傳本地文件
*
* @param bucket Bucket
* @param filePath 本地文件路徑
* @param fileKey 指定OSS文件名,傳空時自動生成UUID
* @return
* @throws FileNotFoundException
*/
public String putObject(String bucket, String filePath, String fileKey) throws FileNotFoundException {
if (fileKey == null) {
fileKey = UUID.randomUUID().toString().replaceAll("-", "") + filePath.substring(filePath.lastIndexOf("."));
}
ossClient.putObject(bucket, fileKey, new File(filePath));
logger.info("OSS putObject success! fileKey={}", fileKey);
return fileKey;
}
/**
* 下載文件到本地目錄
*
* @param bucket Bucket
* @param fileKey OSS文件名
* @param localPath 本地文件目錄
* @return
*/
public String getObject(String bucket, String fileKey, String localPath) {
String localFileKey = localPath + fileKey;
ossClient.getObject(new GetObjectRequest(bucket, fileKey), new File(localFileKey));
logger.info("OSS getObject success! localFileKey={}", localFileKey);
return localFileKey;
}
/**
* 檢查文件是否存在
*
* @param bucket Bucket
* @param fileKey OSS文件名
* @return
*/
public boolean checkObject(String bucket, String fileKey) {
return ossClient.doesObjectExist(bucket, fileKey);
}
/**
* 列出所有文件
*
* @param bucket Bucket
* @param keyPrifix 文件名前綴
*/
public List<String> listObjects(String bucket, String keyPrifix) {
List<String> fileList = new ArrayList<>();
ObjectListing objectListing = ossClient.listObjects(bucket, keyPrifix);
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
fileList.add(bucket);
}
return fileList;
}
/**
* 刪除文件
*
* @param bucket
* @param fileKey
*/
public void deleteObject(String bucket, String fileKey) {
ossClient.deleteObject(bucket, fileKey);
}
private Logger logger = LoggerFactory.getLogger(OssComponentTest.class);
private String localPath;
@Autowired
OssComponent ossComponent;
public OssComponentTest() {
localPath = System.getProperty("user.dir") + "/" + "upload/";
}
@Test
public void urlFromFileKey() throws Exception {
String bucket = "foobar";
String fileKey = "0c2eb1357a454c71b71aa20462951ac4.jpg";
String url = ossComponent.urlFromFileKey(bucket, fileKey);
logger.debug(url);
assertNotNull(url);
}
@Test
public void putObject() throws Exception {
String bucket = "foobar";
String filePath = localPath + "sonic.jpg";
String fileKey = ossComponent.putObject(bucket, filePath, null);
logger.debug(fileKey);
assertNotNull(fileKey);
fileKey = ossComponent.putObject(bucket, filePath, "sonic.jpg");
logger.debug(fileKey);
assertNotNull(fileKey);
}
@Test
public void getObject() throws Exception {
String bucket = "foobar";
String fileKey = "0c2eb1357a454c71b71aa20462951ac4.jpg";
String localPath = this.localPath;
String localFileKey = ossComponent.getObject(bucket, fileKey, localPath);
logger.debug(localFileKey);
assertNotNull(localFileKey);
}
@Test
public void checkObject() throws Exception {
String bucket = "foobar";
String fileKey = "0c2eb1357a454c71b71aa20462951ac4.jpg";
boolean flag = ossComponent.checkObject(bucket, fileKey);
assertTrue(flag);
}
@Test
public void listObjects() throws Exception {
String bucket = "foobar";
String keyPrifix = "";
List<String> list = ossComponent.listObjects(bucket, keyPrifix);
logger.debug(list.toString());
assertTrue(list.size() > 0);
}
@Test
public void deleteObject() throws Exception {
String bucket = "foobar";
String fileKey = "sonic.jpg";
ossComponent.deleteObject(bucket, fileKey);
}