【对象存储】SpringBoot集成华为云OBS对象存储

 总体思路是后端向华为云OBS服务器申请授权信息并设置过期时间,后端再将授权信息以Map形式发给前端,前端将文件和授权信息直接发给华为云OBS服务器。

1. 引入依赖

java 复制代码
		<dependency>
            <groupId>com.huaweicloud</groupId>
            <artifactId>esdk-obs-java</artifactId>
            <version>3.20.6.1</version>
        </dependency>
        
        <dependency>
            <groupId>com.huaweicloud</groupId>
            <artifactId>esdk-obs-java-bundle</artifactId>
            <version>3.23.9</version>
        </dependency>

2. 向前端发送授权码

java 复制代码
package com.example.meetings.controller;


import com.obs.services.ObsClient;
import com.obs.services.model.*;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: ObsController
 * @Description: OBS服务器Controller
 * @Author: wuhuiju
 * @Version: 1.0
 */
@RestController
@RequestMapping({ "/oss" })
public class HweiYunOBSController {

    /**
     * 完整域名为/oss/policy
     * 返回结果
     formParams.put("x-obs-acl", "public-read");
     formParams.put("content-type", "text/plain");
     formParams.put("accessId", accessId);
     formParams.put("policy", response.getPolicy());
     formParams.put("signature", response.getSignature());
     formParams.put("dir", dir);
     formParams.put("host", host);
     */
    @CrossOrigin
    @GetMapping("/policy")
    public Map<String, Object>  policy() {
        // 访问Id
        String accessId = "************************";

        // 访问密钥
        String accessKey = "************************";

        // Endpoint
        String endpoint = "************************";

        // 填写Bucket名称
        String bucket = "pic";

        // 填写Host地址,格式为https://bucketname.endpoint。
        String host = "https://" + bucket + "." + endpoint;

        // 设置上传到OSS文件的前缀,可置空此项
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        // 生成文件夹,以日期为文件夹名
        String dateform = simpleDateFormat.format(new Date());
        String dir = "pic/" + dateform + "/";

        // 创建ObsClient实例
        // 使用永久AK/SK初始化客户端
        ObsClient obsClient = new ObsClient(accessId, accessKey,endpoint);

        try {
            // 生成基于表单上传的请求
            PostSignatureRequest request = new PostSignatureRequest();

            // 返回结果
            Map<String, Object> formParams = new HashMap<String, Object>();

            // 设置对象访问权限为公共读
            formParams.put("x-obs-acl", "public-read");
            // 设置对象MIME类型
            formParams.put("content-type", "image/jpeg");
            request.setFormParams(formParams);

            // 设置表单上传请求有效期,单位:秒
            request.setExpires(3600);
            PostSignatureResponse response = obsClient.createPostSignature(request);

            formParams.put("accessId", accessId);
            formParams.put("policy", response.getPolicy());
            formParams.put("signature", response.getSignature());
            formParams.put("dir", dir);
            formParams.put("host", host);
            return formParams;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
}
相关推荐
浩子coding8 分钟前
通过 Spring AI Alibaba 源码,看如何玩转 ReAct 智能体范式
人工智能·后端
星浩AI19 分钟前
合规项目大模型如何部署?硬件选型 + vLLM/LMDeploy 实战
pytorch·后端·llm
摇滚侠32 分钟前
SpringMVC 入门到实战 DispatcherServlet 源码解读 92-95
java·后端·spring·maven·intellij-idea
码不停蹄的玄黓2 小时前
Spring Bean 生命周期
java·后端·spring
西安邮电大学2 小时前
分治算法详细讲解
java·后端·其他·算法·面试
老马聊技术2 小时前
AI对话功能之SpringBoot整合Vue3
vue.js·人工智能·spring boot·后端
武子康3 小时前
调查研究-174 什么是“红丸主义“:它为什么吸引人,又为什么容易把人带偏?
后端
神奇小汤圆3 小时前
白嫖DeepSeek V4 Pro!免费无限用,还能接入Claude-Code
后端
码不停蹄的玄黓3 小时前
SpringBoot 全局异常处理器实现
java·spring boot·后端
JS菌3 小时前
Skills 动态加载系统:让 AI Agent 按需获取领域知识
前端·人工智能·后端