当前位置: 首页 > article >正文

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

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

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

在OSSUtils类中注入相关属性:

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 "" ;
    }
}

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

@Api(tags = "商品图片表接口")
@RestController
@RequestMapping("")
public class GoodsPicsController {

    @Resource
    protected 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.kler.cn/news/339184.html

相关文章:

  • 数学基础-向量投影
  • 头歌实践教学平台 大数据编程 实训答案(二)
  • Vue基础指令用法
  • 【信息论基础第四讲】信息的流动——平均互信息及其性质
  • Java8新特性, 函数式编程及Stream流用法大全
  • 重装 open-vm-tools
  • 网络通信——OSPF和RIP的区别(总结)
  • 全球IP归属地查询-IP地址查询-IP城市查询-IP地址归属地-IP地址解析-IP位置查询-IP地址查询API接口
  • SAP学习笔记 - Basis01 - 创建Client ,拷贝Client
  • 【图论】迪杰特斯拉算法
  • 【PGCCC】从 PostgreSQL 表恢复已删除的数据 | 翻译
  • 自动机器学习(AutoML):实战项目中的应用与实现
  • 《重生到现代之从零开始的C语言生活》—— 结构体和位段
  • 【分布式微服务云原生】探索MySQL的高级特性:主从复制、读写分离与分库分表
  • pnpm在monorepo架构下不能引用其他模块的问题
  • 【C语言】指针练习题
  • springboot旧物置换网站 (附源码)
  • 亚马逊AI编程工具Amazon Q 和 Amazon CodeWhisperer使用教程
  • Android2024.2.1升级错误
  • HTTP协议:连接世界的语言 —— Python中的实践与探索