<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.15.1</version></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.1</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version></dependency><!-- no more than 2.3.3--><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId><version>2.3.3</version></dependency>
/**
* 上传文件到阿里云
* 基本路径: eg:https://yi-ban-jia.oss-cn-beijing.aliyuncs.com/wuyu.png
*/@ConfigurationProperties(prefix ="aliyun.oss")@ComponentpublicclassAliYunOSSUtil{privatestaticString endpoint;privatestaticString bucketName;privatestaticString accessKeyId;privatestaticString accessKeySecret;publicstaticStringputObject(InputStream inputStream,String suffix){String objectName =UUID.fastUUID()+"."+ suffix;// 创建OSSClient实例。OSS ossClient =newOSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ossClient.putObject(bucketName, objectName, inputStream);returngetBaseUrl()+ objectName;}publicstaticStringputObject(MultipartFile file){String originalFilename = file.getOriginalFilename();String suffix =StringUtils.getFilenameExtension(originalFilename);String objectName =UUID.fastUUID()+"."+ suffix;// 创建OSSClient实例。OSS ossClient =newOSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try{
ossClient.putObject(bucketName, objectName, file.getInputStream());}catch(IOException e){thrownewRuntimeException(e);}returngetBaseUrl()+ objectName;}/**
* 通过 对象名 删除 对象
*
* @param objectName 要删除的文件完整路径。文件完整路径中不能包含Bucket名称
*/publicstaticvoidremoveObject(String objectName){OSS ossClient =null;try{
ossClient =newOSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ossClient.deleteObject(bucketName, objectName);}catch(OSSException oe){System.out.println("Caught an OSSException, which means your request made it to OSS, "+"but was rejected with an error response for some reason.");System.out.println("Error Message:"+ oe.getErrorMessage());System.out.println("Error Code:"+ oe.getErrorCode());System.out.println("Request ID:"+ oe.getRequestId());System.out.println("Host ID:"+ oe.getHostId());}catch(ClientException ce){System.out.println("Caught an ClientException, which means the client encountered "+"a serious internal problem while trying to communicate with OSS, "+"such as not being able to access the network.");System.out.println("Error Message:"+ ce.getMessage());}finally{if(ossClient !=null){
ossClient.shutdown();}}}/**
* 通过 对象名列表批量移除
*
* @param objectNameList
*/publicstaticvoidremoveBatchObjects(List<String> objectNameList){OSS ossClient =null;
ossClient =newOSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(newDeleteObjectsRequest(bucketName).withKeys(objectNameList).withEncodingType("url"));List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();try{for(String obj : deletedObjects){String deleteObj =URLDecoder.decode(obj,"UTF-8");System.out.println(deleteObj);}}catch(UnsupportedEncodingException e){
e.printStackTrace();}catch(OSSException oe){System.out.println("Caught an OSSException, which means your request made it to OSS, "+"but was rejected with an error response for some reason.");System.out.println("Error Message:"+ oe.getErrorMessage());System.out.println("Error Code:"+ oe.getErrorCode());System.out.println("Request ID:"+ oe.getRequestId());System.out.println("Host ID:"+ oe.getHostId());}catch(ClientException ce){System.out.println("Caught an ClientException, which means the client encountered "+"a serious internal problem while trying to communicate with OSS, "+"such as not being able to access the network.");System.out.println("Error Message:"+ ce.getMessage());}finally{if(ossClient !=null){
ossClient.shutdown();}}}publicstaticStringgetBaseUrl(){return"https://"+ bucketName +"."+ endpoint.substring(8)+"/";}publicvoidsetEndpoint(String endpoint){AliYunOSSUtil.endpoint = endpoint;}publicvoidsetBucketName(String bucketName){AliYunOSSUtil.bucketName = bucketName;}publicvoidsetAccessKeyId(String accessKeyId){AliYunOSSUtil.accessKeyId = accessKeyId;}publicvoidsetAccessKeySecret(String accessKeySecret){AliYunOSSUtil.accessKeySecret = accessKeySecret;}}