1、文件上傳
public ReturnMessage<Map<String,Object>> uploadFile(String ossFileKey,File file ) throws Exception {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
PutObjectResult putObjectResult = ossClient.putObject(new PutObjectRequest(bucketName, ossFileKey, file));
if(putObjectResult!=null){
// 設置URL過期時間爲1小時。
Date expiration = new Date(System.currentTimeMillis() + OssConstants.URL_EXPIRATION_TIME);
//生成以GET方法訪問的簽名URL,訪客可以直接通過瀏覽器訪問相關內容。
URL url = ossClient.generatePresignedUrl(bucketName, ossFileKey, expiration);
Map<String,Object> rtnValue = new HashMap<>();
rtnValue.put("url",url.toString());
rtnValue.put("ossFileKey", ossFileKey);
rtnValue.put("name",file.getName());
return new ReturnMessage<>(true,"" ,rtnValue);
}else{
return new ReturnMessage<>(false,ResultStatusConstants.SYSTEM_ERROR);
}
} catch (OSSException oe) {
log.error("Error Message : " + oe.getMessage() ,oe);
throw oe;
} catch (ClientException ce) {
log.error("Error Message : " + ce.getMessage() ,ce);
throw ce;
} finally {
ossClient.shutdown();
}
}