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

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

1. 引入依赖

		<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. 向前端发送授权码

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

相关推荐

  1. 对象存储SpringBoot集成华为OBS对象存储

    2024-04-26 12:46:02       15 阅读
  2. SpringBoot中实现阿里OSS对象存储

    2024-04-26 12:46:02       34 阅读
  3. Hadoop集成对象存储和HDFS磁盘文件存储

    2024-04-26 12:46:02       43 阅读
  4. 阿里对象存储(OSS)服务

    2024-04-26 12:46:02       42 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-26 12:46:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-26 12:46:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-26 12:46:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-26 12:46:02       20 阅读

热门阅读

  1. Vue3全局Api应用实例

    2024-04-26 12:46:02       12 阅读
  2. 资源、死锁、如何监测死锁

    2024-04-26 12:46:02       12 阅读
  3. 树莓派在人工智能领域的应用探索

    2024-04-26 12:46:02       13 阅读
  4. C语言经典例题-11

    2024-04-26 12:46:02       10 阅读
  5. 【Go】通道作为函数参数

    2024-04-26 12:46:02       11 阅读
  6. C++ 内存管理

    2024-04-26 12:46:02       10 阅读
  7. 白帽子讲Web安全读书笔记

    2024-04-26 12:46:02       10 阅读
  8. AI电销机器人系统源码部署之:freeswitch安装Linux

    2024-04-26 12:46:02       16 阅读