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("上传失败!");}}
🔥博客主页:fly in the sky - CSDN博客 🚀欢迎各位:点赞👍收藏⭐️留言✍️🚀 🎆慢品人间烟火色,闲观万事岁月长🎆 📖希望我写的博客对你有所帮助,如有不足,请指正&#…
0魔法阵 - 蓝桥云课 (lanqiao.cn) #include <iostream>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm>using namespace std;#define x first
#define y second
const int N 1010;
const int inf 1e4;
vector&…