spring cloud aws如何使用S3协议

公司最近用minio 生产环境也使用支持S3协议的存储服务。spring cloud aws 就支持S3协议。

  1. 第一步引入相关jar包
<dependency>
    <groupId>io.awspring.cloud</groupId>
    <artifactId>spring-cloud-aws-dependencies</artifactId>
    <version>2.3.2</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>
<dependency>
    <groupId>io.awspring.cloud</groupId>
    <artifactId>spring-cloud-starter-aws</artifactId>
</dependency>
  1. 添加配置文件
cloud:
  aws:
    credentials:
      access-key: xxxxx
      secret-key:xxxxx
    s3:
      endpoint: http://xxxxx
  1. 下载文件
    可以通过使用s3协议引用 Amazon S3 存储桶和存储桶内的对象来下载文件。典型的模式是s3:///,bucket 是全局唯一的bucket 名称,object 是bucket 内的有效对象名称。对象名称可以是存储桶根文件夹中的文件,也可以是存储桶内目录中的嵌套文件。

下一个示例演示如何使用资源加载器加载不同的资源。

public class SimpleResourceLoadingBean {

    @Autowired
    private ResourceLoader resourceLoader;

    public void resourceLoadingMethod() throws IOException {
        Resource resource = this.resourceLoader.getResource("s3://myBucket/rootFile.log");
        Resource secondResource = this.resourceLoader.getResource("s3://myBucket/rootFolder/subFile");

        InputStream inputStream = resource.getInputStream();
        //read file
    }
}

4.上传文件
从 Spring Framework 3.1 开始,资源加载器也可以用来上传带有org.springframework.core.io.WritableResource 接口的文件,这是接口的特殊化org.springframework.core.io.ResourceLoader。客户端可以使用该WritableResource界面上传文件。下一个示例演示使用资源加载器上传资源。

public class SimpleResourceLoadingBean {

    @Autowired
    private ResourceLoader resourceLoader;

    public void writeResource() throws IOException {
        Resource resource = this.resourceLoader.getResource("s3://myBucket/rootFile.log");
        WritableResource writableResource = (WritableResource) resource;
        try (OutputStream outputStream = writableResource.getOutputStream()) {
            outputStream.write("test".getBytes());
        }
    }
}
  1. 使用 TransferManager 上传
    Amazon SDK 还提供了一个对上传文件非常有用的高级抽象,还提供了使用多部分功能的多线程。com.amazonaws.services.s3.transfer.TransferManager可以在应用程序代码中轻松创建A并注入com.amazonaws.services.s3.AmazonS3已使用 Spring Cloud AWS 资源加载器配置创建的预配置客户端。

此示例显示transferManager在应用程序中使用 来从硬盘驱动器上传文件。

public class SimpleResourceUploadingBean {

    @Autowired
    private AmazonS3 amazonS3;

    public void withTransferManager() {
        TransferManager transferManager = TransferManagerBuilder.standard()
                                                                .withS3Client(this.amazonS3)
                                                                .build();
        transferManager.upload("myBucket","filename",new File("someFile"));
    }
}
  1. 获取外网下载地址
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName,ossPath).withMethod(HttpMethod.Get).withExpiration(过期时间);
String url = amazonS3.generatePresignedUrl(request)+"";

spring cloud aws官方文档

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值