packagecom.zhx.app.utils;importcom.aliyun.oss.ClientException;importcom.aliyun.oss.OSS;importcom.aliyun.oss.OSSClientBuilder;importcom.aliyun.oss.OSSException;importcom.aliyun.oss.model.PutObjectRequest;importcom.aliyun.oss.model.PutObjectResult;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Component;importjava.io.IOException;importjava.io.InputStream;/*** @ClassName : AliOssUtil* @Description : 阿里云上传服务* @Author : zhx* @Date: 2024-03-1 20:29*/@ComponentpublicclassAliOssUtil{privatestaticStringENDPOINT;@Value("${alioss.endpoint}")publicvoidsetENDPOINT(String endpoint){ENDPOINT= endpoint;}privatestaticStringACCESS_KEY;@Value("${alioss.access_key}")publicvoidsetAccessKey(String accessKey){ACCESS_KEY= accessKey;}privatestaticStringACCESS_KEY_SECRET;@Value("${alioss.access_key_secret}")publicvoidsetAccessKeySecret(String accessKeySecret){ACCESS_KEY_SECRET= accessKeySecret;}privatestaticStringBUCKETNAME;@Value("${alioss.bucketName}")publicvoidsetBUCKETNAME(String bucketName){BUCKETNAME= bucketName;}publicstaticStringuploadFile(String objectName,InputStream inputStream){String url ="";// 创建OSSClient实例。OSS ossClient =newOSSClientBuilder().build(ENDPOINT,ACCESS_KEY,ACCESS_KEY_SECRET);try{// 创建PutObjectRequest对象。PutObjectRequest putObjectRequest =newPutObjectRequest(BUCKETNAME, objectName, inputStream);// 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。// ObjectMetadata metadata = new ObjectMetadata();// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());// metadata.setObjectAcl(CannedAccessControlList.Private);// putObjectRequest.setMetadata(metadata);// 上传文件。PutObjectResult result = ossClient.putObject(putObjectRequest);url ="https://"+BUCKETNAME+"."+ENDPOINT.substring(ENDPOINT.lastIndexOf("/")+1)+"/"+ 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();}}return url;}publicstaticResultVodeleteFile(String objectName){// 创建OSSClient实例。OSS ossClient =newOSSClientBuilder().build(ENDPOINT,ACCESS_KEY,ACCESS_KEY_SECRET);try{// 删除文件。ossClient.deleteObject(BUCKETNAME, objectName);returnResultUtils.success("删除成功!");}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();}}returnResultUtils.error("上传失败!");}}