keyfil/文档/基础组件/oss/20250530-engine-starter-oss-2.0.0.md
liuxiaohua e3703d69f3
All checks were successful
Publish to Confluence / confluence (push) Successful in 1m7s
[2025-05-30] 添加 oss 使用说明
2025-05-30 18:06:40 +08:00

3.8 KiB

engine-starter-oss 使用教程

这是一个基于 aws-s3-client 封装的 oss 存储对接项目

特性

  • YmOssTemplateContext 消息发送工具类

快速开始

  • 注意: qifu-saas-parent >= 2.0.0-SNAPSHOT

添加依赖


<dependency>
    <groupId>com.yuanmeng.engine</groupId>
    <artifactId>engine-starter-oss</artifactId>
    <version>2.0.0-SNAPSHOT</version>
</dependency>

配置 OSS

yuanmeng:
  oss:
    #- 阿里云
    endpoint: https://oss-cn-shenzhen.aliyuncs.com
    region: cn-shenzhen
    access-key: xxx
    secret-key: xxx
    bucket-name: xxx
    path-style-access: false
    configs:
      #- minio
      minIoOssTemplate:
        endpoint: http://local.com:9000
        region: cn-shenzhen
        access-key: xxx
        secret-key: xxx
        bucket-name: aws-s3-client
        path-style-access: true

工具使用

文件存储使用


@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/oss")
public class TestController {

    @PostMapping("/upload")
    public String upload(MultipartFile file,
                         @RequestParam(value = "client", required = false) String client) throws IOException {
        log.info(YmThreadLocalUtils.getTokenInfoString());
        PutObjectResponse local = YmOssTemplateContext.load(client)
                .putObject(file.getOriginalFilename(), file.getInputStream());
        return local.toString();
    }

    @GetMapping("/url-get")
    public String getUrl(@RequestParam("name") String objectName,
                         @RequestParam(value = "client", required = false) String client) {
        log.info(YmThreadLocalUtils.getTokenInfoString());
        return YmOssTemplateContext.load(client).getObjectURL(objectName);
    }

    @GetMapping("/url-get-presigned")
    public String getPresignedUrl(@RequestParam("name") String objectName,
                                  @RequestParam(value = "client", required = false) String client) {
        log.info(YmThreadLocalUtils.getTokenInfoString());
        return YmOssTemplateContext.load(client).getPresignedURL(objectName, 10);
    }

    @GetMapping("/url-post-presigned")
    public String getPostPresignedUrl(@RequestParam("name") String objectName,
                                      @RequestParam(value = "client", required = false) String client) {
        log.info(YmThreadLocalUtils.getTokenInfoString());
        return YmOssTemplateContext.load(client).getPutPresignedURL(objectName, 10);
    }

}

完整配置

yuanmeng:
  oss:
    #- 阿里云
    endpoint: https://oss-cn-shenzhen.aliyuncs.com
    region: cn-shenzhen
    access-key: xxx
    secret-key: xxx
    bucket-name: xxx
    path-style-access: false
    configs:
      #- minio
      minIoOssTemplate:
        endpoint: http://local.com:9000
        region: cn-shenzhen
        access-key: xxx
        secret-key: xxx
        bucket-name: aws-s3-client
        path-style-access: true
      #- qiniu
      qiNiuOssTemplate:
        endpoint: xxx
        region: cn-shenzhen
        access-key: xxx
        secret-key: xxx
        bucket-name: aws-s3-client
        path-style-access: true
      #- 其他支持 s3 协议的 oss 存储