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右侧点击"购买资源包",选择合适的档位,付费购买。
右侧点击"新建Bucket",输入一个全局唯一的bucket名,并选择所属地域,新建Bucket。
左侧列表中,进入新建的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);
}