SpringBoot中如何集成OSS对象存储服务

news/2024/10/8 23:23:20/

首先在application.yml中配置相关信息

# oss配置
oss:endpoint: oss-cn-hangzhou.aliyuncs.comaccessKeyId: LTAI5tHmWEh4nWZD*******accessKeySecret: 5dywdOaBWzNsGXUvv0*******bucketName: monian-web-tliasbaseURL: https://monian-web-tlias.oss-cn-hangzhou.aliyuncs.com/

在OSSUtils类中注入相关属性:

java">import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.UUID;@Component
@Data
@ConfigurationProperties(prefix = "oss")
public class OSSUtil {private String endpoint ;private String accessKeyId ;private String accessKeySecret ;private String bucketName ;private String baseURL ;public String upload(byte[] dataAy,String extendName) throws IOException {//创建 OSS对象OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {//上传oss 文件夹final String keySuffixWithSlash = "goods-pic/"+ UUID.randomUUID().toString()+"."+extendName;client.putObject(bucketName, keySuffixWithSlash, new ByteArrayInputStream(dataAy));System.out.println("Creating an empty folder " + keySuffixWithSlash + "\n");return  baseURL+keySuffixWithSlash ;} 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 {/** Do not forget to shut down the client finally to release all allocated resources.*/client.shutdown();}return "" ;}
}

调用工具类进行图片的上传;

java">@Api(tags = "商品图片表接口")
@RestController
@RequestMapping("")
public class GoodsPicsController {@Resourceprotected OSSUtil ossUtil ;@PostMapping("/file/upload")public AjaxResult upload(@RequestParam("file") MultipartFile file) throws Exception{//获取文件后缀+文件名String oldName = file.getOriginalFilename();//将后缀加到新的文件名上String extendName =  oldName.substring(oldName.lastIndexOf(".")+1);// 获取文件的字节byte[] bytes = file.getBytes();// 这里可以添加保存文件的代码,例如将文件保存到服务器的指定目录String fileName = ossUtil.upload(bytes,extendName);return success("上传成功!",fileName);}
}


http://www.ppmy.cn/news/1536356.html

相关文章

LeetCode 209 Minimum Size Subarray Sum 题目解析和python代码

题目: Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead. Example 1: Input: target 7, nu…

HarmonyOS鸿蒙 Next 实现协调布局效果

HarmonyOS鸿蒙 Next 实现协调布局效果 ​ 假期愉快! 最近大A 的涨势实在是红的让人晕头转向,不知道各位收益如何,这会是在路上,还是已经到目的地了? 言归正传,最近有些忙,关于鸿蒙的实践系列有些脱节了,…

云原生架构师2024

├──2-Linux操作系统 | ├──1-项目部署之-Linux操作系统 | | ├──1-Linux概述与安装 | | ├──2-Linux基本操作 | | └──3-Linux软件安装与配置 | └──2-Shell编程 | | └──1-Shell编程 | ├──3-计算机网络基础 | | └──1-计算机…

贝锐蒲公英工业物联方案:助力美的智慧楼宇全球布局

智慧楼宇正日益成为现代城市发展的基石,作为该领域的先锋,美的楼宇科技通过其创新的iBUILDING数字化平台和低碳技术,引领着智慧空间的可持续发展,并持续推动建筑及相关行业的数字化转型。 美的楼宇科技的解决方案融合了先进的楼宇…

eNodeB User Manual - Troubleshooting

### COTS UE问题 以下是使用srsENB时最常见的问题: #### UE看不到网络 UE看不到网络的最可能原因是eNB/EPC配置、RF条件和使用的射频前端的频率精度。 首先检查您配置的LTE频段和EARFCN是否被您正在使用的UE支持。大多数UE设备支持LTE分配的频段的一个子集。确保…

React事件机制详解

React的事件机制详解如下: 1. 事件绑定 在React中,事件绑定是通过JSX语法实现的,例如使用onClick、onChange等属性来绑定点击事件或输入框内容改变事件等。 2. 事件处理程序 事件处理程序是在事件触发时执行的函数,这些函数被定义…

MyBatis 如何实现延迟加载?深度探讨 MyBatis 的延迟加载:如何优化数据访问效率

在当今的应用程序开发中,尤其是与数据库交互时,性能成为了重中之重。频繁的数据库访问会导致响应时间变慢,甚至影响用户体验。为了优化数据访问,MyBatis 提供了延迟加载(Lazy Loading)的强大功能。本文将详…

linux批量删文件

在 Linux 中,可以使用命令行工具来批量删除文件。以下是一些常用的方法: 使用 rm 命令 rm 是一个用于删除文件和目录的命令。使用此命令时应谨慎,因为删除操作是不可逆的。 删除特定类型的文件 例如,要删除当前目录下所有的 .tx…