对象存储使用高级指南之兼容各种S3协议下的对象存储(自己搭建的Minio,阿里云的oss等)

S3协议对象存储封装指南

一、引言

在使用对象存储过程中,因为客户要求不同,可能有的客户感觉放在阿里云上也可以,但是有的客户需要高度私有化部署。面对不同的需求我们就需要对接不同的对象存储方式,比如阿里云的OSS,自己搭建的MinIo等等。这些对象存储大多数都遵循S3协议(S3协议详细内容可百度查阅)。所以我们只需针对S3协议相关内容对接阿里云等对象存储即可,以下内容给是我自己封装的对象存储工具类。

使用说明

在我封装的工具类中有一个客户端直传,具体什么是客户端直传详见:点击查看文章

二、依赖引入

Gradle

implementation("com.amazonaws:aws-java-sdk-s3:1.12.700")

Maven

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
    <version>1.12.700</version>
</dependency>

三、相关配置

s3:
    accessKeyId: ${ACCESS_KEY_ID}
    accessKeySecret: ${ACCESS_KEY_SECRET}
    bucketName: ${BUCKET_NAME}
    endpoint: https://oss-cn-hangzhou.aliyuncs.com
    expireTime: 10
    region: oss-cn-hangzhou

四、具体封装工具类

import cn.hutool.core.util.StrUtil;
import com.amazonaws.HttpMethod;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import jakarta.annotation.PostConstruct;
import lombok.CustomLog;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.File;
import java.net.URL;
import java.util.Date;

@Component
@Data
@CustomLog
@ConfigurationProperties(prefix = "s3")
public class S3Util {

    private String endpoint;

    private String accessKeyId;

    private String accessKeySecret;

    private String bucketName;

    private long expireTime;

    private String region;

    private AmazonS3 amazonS3;

    @PostConstruct
    public void init() {
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKeyId, accessKeySecret);
        //创建amazonS3
        this.amazonS3 = AmazonS3Client.builder()
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region))
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .disableChunkedEncoding()
                .withPathStyleAccessEnabled(false)
                .build();
    }


    /**
     * 获取客户端直传url
     * @return
     */
    @SneakyThrows
    public String generateUploadUrl(String pathAndName) {
        Date expiration = new Date(System.currentTimeMillis() + expireTime * 60 * 1000);
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, pathAndName, HttpMethod.PUT);
        request.setExpiration(expiration);
        URL url = amazonS3.generatePresignedUrl(request);
        return url.toString();
    }

    /**
     * 获取客户端直接下载url
     * @return
     */
    @SneakyThrows
    public String generateDownloadUrl(String pathAndName) {
        Date expiration = new Date(System.currentTimeMillis() + expireTime * 60 * 1000);
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, pathAndName)
                .withMethod(HttpMethod.GET)
                .withExpiration(expiration);
        if (!amazonS3.doesObjectExist(bucketName, pathAndName)) {
            throw ErrorUtil.make("文件不存在");
        }
        return amazonS3.generatePresignedUrl(request).toString();
    }

    /**
     * 批量更改文件所在文件夹位置
     * @param sourceKey 原始key
     * @param targetKey 目标key
     */
    @SneakyThrows
    public void changePath(String sourceKey, String targetKey){
        amazonS3.copyObject(bucketName, sourceKey, bucketName, targetKey);
        // 删除旧文件
        amazonS3.deleteObject(bucketName, sourceKey);
    }


    /**
     * 文件下载
     * @param folder 文件所属目录
     * @param fileName 文件名称
     * @param targetPath 文件下载地址
     */
    @SneakyThrows
    public void downLoadFile(String folder, String fileName, String targetPath){
        amazonS3.getObject(
                new GetObjectRequest(
                        bucketName,
                        StrUtil.concat(false, folder, fileName)
                ),
                new File(StrUtil.concat(false, targetPath, fileName))
        );
    }

    /**
     * 文件删除
     * @param pathAndName 删除的文件
     */
    @SneakyThrows
    public void deleteFile(String pathAndName){
        amazonS3.deleteObject(bucketName, pathAndName);
        if (amazonS3.doesObjectExist(bucketName, pathAndName)) {
            throw ErrorUtil.make("删除失败");
        }
    }

    /**
     * 检查文件是否存在
     * @param pathAndName 删除的文件
     */
    @SneakyThrows
    public boolean doesFileExist(String pathAndName){
        return amazonS3.doesObjectExist(bucketName, pathAndName);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值