三、七牛云对象存储,AliossTemplate的封装使用

一、MINIO的安装,MinioTemplate的封装使用
二、阿里云OSS,AliossTemplate的封装使用
三、七牛云对象存储,QiniuTemplate的封装使用
四、华为云OBS对象存储,HwObsTemplate的封装使用
五、腾讯COS对象存储,TxCosTemplate的封装使用

三、七牛云对象存储,QiniuTemplate的封装使用

在上一章《阿里云OSS,AliossTemplate的封装使用》对阿里云存储进行可简单的介绍封装。本节对七牛云对象存储同样基于MINIIO的思路,对桶的创建删除,文档的创建、删除、文件拷贝进行封装。

1、获取服务

七牛云官方赠送了10G的流量包,不用购买,开通这项服务就可以了。

在下面的网页中获取endpoint地址,这个地址是临时的,你需要在域名管理中配置自己的域名。

在这里插入图片描述

在下面页面中获取凭证

在这里插入图片描述

2、Java代码封装QiniuTemplate,操作七牛云对象存储
2.1 引入依赖
<!--QiNiu-->
<dependency>
    <groupId>com.qiniu</groupId>
    <artifactId>qiniu-java-sdk</artifactId>
    <scope>provided</scope>
</dependency>
2.2 代码结构如下

下面两个章节将不再有结构贴图,用了一天时间,将七牛、腾讯、华为的对象存储做了整体的封装测试。

在这里插入图片描述

2.3 QiniuTemplate代码
package com.toto.core.oss;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.qiniu.common.Zone;
import com.qiniu.http.Client;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.toto.core.oss.enums.UploadType;
import com.toto.core.oss.model.TotoFile;
import com.toto.core.oss.tool.OssUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: QiniuTemplate
 * @Package: com.toto.core.oss
 * @Author gufanbiao
 * @CreateTime 2024-06-01 09:21
 */
@SuppressWarnings("all")
@AllArgsConstructor
public class QiniuTemplate implements OssTemplate {

    private final Auth auth;
    private final UploadManager uploadManager;
    private final BucketManager bucketManager;
    private final OssProperties ossProperties;
    private final Client client;

    @Override
    public void makeBucket(String bucketName) throws Exception{
        if (ObjectUtil.isNull(bucketManager.buckets()) || !CollectionUtil.contains(Arrays.asList(bucketManager.buckets()), bucketName)) {
            bucketManager.createBucket(bucketName, Zone.autoZone().getRegion());
        }
    }

    @Override
    public void deleteBucket(String bucketName) throws Exception {
        String url = "https://uc.qiniuapi.com/drop/" + bucketName;
        StringMap headers = this.auth.authorizationV2(url, "POST", null, "application/x-www-form-urlencoded");
        client.post(url, null, headers, "application/x-www-form-urlencoded");
    }

    @Override
    public boolean bucketExists(String bucketName) throws Exception {
        return ObjectUtil.isNotNull(bucketManager.buckets()) && CollectionUtil.contains(Arrays.asList(bucketManager.buckets()), bucketName);
    }

    @Override
    public void copyFile(String bucketName, String fileName, String destBucketName) throws Exception {
        bucketManager.copy(bucketName, fileName, destBucketName, fileName);
    }

    @Override
    public void copyFile(String bucketName, String fileName, String destBucketName, String destFileName) throws Exception {
        bucketManager.copy(bucketName, fileName, destBucketName, destFileName);
    }

    @Override
    public TotoFile getFile(String fileName) throws Exception {
        return getFile(ossProperties.getBucketName(), fileName);
    }

    @Override
    public TotoFile getFile(String bucketName, String fileName) throws Exception {
        FileInfo fileInfo = bucketManager.stat(bucketName, fileName);
        TotoFile file = new TotoFile();
        file.setFileName(ObjectUtil.isNotNull(fileInfo.key) ? fileInfo.key : fileName);
        file.setFileUrl(fileUrl(file.getFileName()));
        file.setHash(fileInfo.hash);
        file.setFileSize(fileInfo.fsize);
        file.setUploadTime(new Date(fileInfo.putTime / 10000));
        file.setContentType(fileInfo.mimeType);
        return file;
    }

    @Override
    public String getPath(String fileName) throws Exception {
        return ossProperties.getBucketName().concat("/").concat(fileName);
    }

    @Override
    public String getPath(String bucketName, String fileName) throws Exception {
        return bucketName.concat("/").concat(fileName);
    }

    @Override
    public String fileUrl(String fileName) throws Exception {
        return ossProperties.getEndpoint().concat("/").concat(ossProperties.getBucketName()).concat("/").concat(fileName);
    }

    @Override
    public String fileUrl(String bucketName, String fileName) throws Exception {
        return ossProperties.getEndpoint().concat("/").concat(bucketName).concat("/").concat(fileName);
    }

    @Override
    public String getShortlytUrl(String bucketName, String fileName, Integer expires) throws Exception {
        //Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
        return auth.privateDownloadUrl("http://"+ ossProperties.getEndpoint() + "/" + fileName,30);
    }

    @Override
    public TotoFile putFile(MultipartFile file) throws Exception {
        return putFile(ossProperties.getBucketName(), file.getOriginalFilename(), file);
    }

    @Override
    public TotoFile putFile(String fileName, MultipartFile file) throws Exception {
        return putFile(ossProperties.getBucketName(), fileName, file);
    }

    @Override
    public TotoFile putFile(String bucketName, String fileName, MultipartFile file) throws Exception {
        return putFile(bucketName, fileName, file.getInputStream(), file);
    }

    @Override
    public TotoFile putFile(String fileName, InputStream stream) throws Exception {
        return putFile(ossProperties.getBucketName(), fileName, stream);
    }

    @Override
    public TotoFile putFile(String bucketName, String fileName, InputStream stream) throws Exception {
        return putFile(bucketName, fileName, stream, "application/octet-stream",null);
    }

    @Override
    public TotoFile putFile(String bucketName, String fileName, InputStream stream, MultipartFile file) throws Exception {
        return putFile(bucketName, fileName, stream, "application/octet-stream", file);
    }

    private TotoFile putFile(String bucketName, String fileName, InputStream stream, String contentType, MultipartFile multipartFile) throws Exception {
        makeBucket(bucketName);
        String originalName = fileName;
        fileName = OssUtil.fileName(fileName);
        // 这里使用覆盖上传
        uploadManager.put(stream, fileName, getUploadToken(bucketName, fileName), null, null);
        TotoFile file = new TotoFile();
        file.setOriginalName(originalName);
        file.setFileName(fileName);
        file.setDomain(OssUtil.getOssHost(ossProperties,bucketName));
        file.setFileUrl(fileUrl(bucketName, fileName));
        file.setFileType(originalName.substring(originalName.lastIndexOf(".") + 1));
        file.setUploadType(UploadType.OSS.getType());
        if(ObjectUtil.isNotNull(multipartFile)){
            file.setFileSize(multipartFile.getSize());
            file.setContentType(multipartFile.getContentType());
            file.setUploadTime(new Date());
        }
        return file;
    }

    @Override
    public void deleteFile(String fileName) throws Exception {
        deleteFile(ossProperties.getBucketName(), fileName);
    }

    @Override
    public void deleteFile(String bucketName, String fileName) throws Exception {
        bucketManager.delete(bucketName, fileName);
    }

    @Override
    public void deleteFiles(List<String> fileNames) throws Exception {
        for (String fileName:fileNames) {
            deleteFile(fileName);
        }
    }

    @Override
    public void deleteFiles(String bucketName, List<String> fileNames) throws Exception {
        for (String fileName:fileNames) {
            deleteFile(bucketName, fileName);
        }
    }

    /**
     * 获取上传凭证,普通上传
     */
    public String getUploadToken(String bucketName) {
        return auth.uploadToken(bucketName);
    }

    /**
     * 获取上传凭证,覆盖上传
     */
    private String getUploadToken(String bucketName, String key) {
        return auth.uploadToken(bucketName, key);
    }

    /**
     * 获取域名
     *
     * @return String
     */
    public String getOssHost() {
        return ossProperties.getEndpoint();
    }
}

3.4 QiniuTest代码
package com.toto;

import com.alibaba.fastjson.JSONObject;
import com.qiniu.http.Client;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.toto.core.oss.OssProperties;
import com.toto.core.oss.QiniuTemplate;
import com.toto.core.oss.model.TotoFile;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: QiniuTest
 * @Package: com.toto
 * @Author gufanbiao
 * @CreateTime 2024-06-01 09:46
 */
@SpringBootTest
@SuppressWarnings("all")
public class QiniuTest {
    private static String ENDPOINT = "seduduson.hd-bkt.clouddn.com";
    private static String ACCESS_KEY = "DS8RTGML1h2NXM9X7fEOSC8bcBN0t9nZTorRWhN8Vi";
    private static String SECRET_KEY = "uY5mNe336YsCEXZDXomvgYCTY46SXK28RtqZrLVrdC";
    private static String BUCKET_NAME = "toto-test";
    private static String BUCKET_DEST_NAME = "toto-dest";

    public static QiniuTemplate template() {
        // 创建配置类
        OssProperties ossProperties = new OssProperties();
        ossProperties.setEndpoint(ENDPOINT);
        ossProperties.setAccessKey(ACCESS_KEY);
        ossProperties.setSecretKey(SECRET_KEY);
        ossProperties.setBucketName(BUCKET_NAME);

        Configuration cfg = new Configuration(Region.autoRegion());
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        UploadManager uploadManager = new UploadManager(cfg);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        Client client = new Client();
        // 创建客户端
        return new QiniuTemplate(auth, uploadManager, bucketManager, ossProperties,client);
    }

    /*********************************** mock对象信息 *****************************************/
    private static String FILE_NAME = "你好世界.txt";
    private static String CONTENT_TYPE = "text/plain";
    private static String CONTENT = "你好世界!";
    /**
     * mock一个对象 使用Spring框架的MockMultipartFile类创建一个MultipartFile对象
     * @return
     * @throws IOException
     */
    public static MultipartFile createMultipartFile() throws IOException {
        return new MockMultipartFile(FILE_NAME, FILE_NAME, CONTENT_TYPE, CONTENT.getBytes());
    }

    /*********************************** 操作测试 *****************************************/
    /**
     * 创建 Bucket
     */
    @Test
    void testCreate() throws Exception {
        template().makeBucket(BUCKET_NAME);
    }

    /**
     * 删除 Bucket,桶不存在报错
     */
    @Test
    void deleteBucket() throws Exception {
        template().deleteBucket(BUCKET_NAME);
    }

    /**
     * 存储桶是否存在
     */
    @Test
    void bucketExists() throws Exception {
        boolean b = template().bucketExists(BUCKET_NAME);
        System.out.println("存储桶是否存在:" + b);
    }

    /**
     * 上传文件 MultipartFile格式
     */
    @Test
    void putFile1() throws Exception {
        TotoFile totoFile = template().putFile(createMultipartFile());
        System.out.println("上传文件对象封装:" + JSONObject.toJSON(totoFile));
    }

    /**
     * 上传文件 文件流模式
     */
    @Test
    void putFile2() throws Exception {
        InputStream inputStream = createMultipartFile().getInputStream();
        TotoFile totoFile = template().putFile(FILE_NAME, inputStream);
        System.out.println("上传文件对象封装:" + JSONObject.toJSON(totoFile));
    }

    /**
     * 上传文件 文件流模式
     */
    @Test
    void putFile3() throws Exception {
        TotoFile totoFile = template().putFile("hhhhhhh"+FILE_NAME, createMultipartFile());
        System.out.println("上传文件对象封装:" + JSONObject.toJSON(totoFile));
    }

    /**
     * 文件复制到另一个桶(文件名/路径不变)
     * @throws Exception
     */
    @Test
    void copyFile1() throws Exception {
        template().copyFile(BUCKET_NAME,"upload/2024-06-01/2a78f1088ea54fd399246fc2778c836a.txt",BUCKET_DEST_NAME);
    }

    /**
     * 文件复制到另一个桶(文件名/路径变更)
     * @throws Exception
     */
    @Test
    void copyFile2() throws Exception {
        template().copyFile(BUCKET_NAME,"upload/2024-06-01/2a78f1088ea54fd399246fc2778c836a.txt",BUCKET_DEST_NAME,"upload1/2024-06-01/2a78f1088ea54fd399246fc2778c836a.txt");
    }

    /**
     * 获取文件信息
     * @throws Exception
     */
    @Test
    void statFile1() throws Exception {
        TotoFile totoFile = template().getFile("upload/2024-06-01/2a78f1088ea54fd399246fc2778c836a.txt");
        System.out.println("获取文件对象信息:" + JSONObject.toJSONString(totoFile));
    }

    /**
     * 获取文件信息(指定桶的名称)
     * @throws Exception
     */
    @Test
    void statFile2() throws Exception {
        TotoFile totoFile = template().getFile(BUCKET_DEST_NAME,"upload/2024-06-01/2a78f1088ea54fd399246fc2778c836a.txt");
        System.out.println("获取文件对象信息:" + JSONObject.toJSONString(totoFile));
    }

    /**
     * 获取文件路径信息(没有什么实质的参考,就是内部组装)
     */
    @Test
    void getPath1() throws Exception {
        String path = template().getPath("upload/2024-06-01/2a78f1088ea54fd399246fc2778c836a.txt");
        System.out.println("获取文件路径信息:" + path);
    }

    /**
     * 获取文件路径信息(没有什么实质的参考,就是内部组装)
     */
    @Test
    void getPath2() throws Exception {
        String path = template().getPath(BUCKET_NAME,"upload/2024-06-01/2a78f1088ea54fd399246fc2778c836a.txt");
        System.out.println("获取文件路径信息:" + path);
    }

    /**
     * 获取文件路径信息(没有什么实质的参考,就是内部组装)
     */
    @Test
    void getFileUrl1() throws Exception {
        String fileUrl = template().fileUrl("upload/2024-06-01/2a78f1088ea54fd399246fc2778c836a.txt");
        System.out.println("获取文件路径信息:" + fileUrl);
    }

    /**
     * 获取文件路径信息(没有什么实质的参考,就是内部组装)
     */
    @Test
    void getFileUrl2() throws Exception {
        String fileUrl = template().fileUrl(BUCKET_NAME,"upload/2024-06-01/2a78f1088ea54fd399246fc2778c836a.txt");
        System.out.println("获取文件路径信息:" + fileUrl);
    }

    //@Test
    //void getPolicyType() throws Exception {
    //    String policyType = template().getPolicyType(BUCKET_NAME, null);
    //    System.out.println("获取当前桶的策略:" + policyType);
    //}

    /**
     * 删除文件,使用配置文件中的桶
     * @throws Exception
     */
    @Test
    void deleteFile1() throws Exception {
        template().deleteFile("upload/2024-06-01/2a78f1088ea54fd399246fc2778c836a.txt");
    }

    /**
     * 删除文件指定桶
     * @throws Exception
     */
    @Test
    void deleteFile2() throws Exception {
        template().deleteFile(BUCKET_NAME,"upload/2024-06-01/e652b226bf6245b598df1871ed473ec8.txt");
    }

    /**
     * 批量删除,默认配置文件中的桶(貌似有点问题,有时候可以,有时候不行)
     * @throws Exception
     */
    @Test
    void deleteFile3() throws Exception {
        List<String> fileNames = new ArrayList<>();
        fileNames.add("upload/2024-06-01/179a2d2768dd424fbef560e87645b7b2.txt");
        fileNames.add("upload/2024-06-01/1a1d040d9cac4063a7524921fa8915d8.txt");
        template().deleteFiles(fileNames);
    }

    /**
     * 获取临时地址,临时地址使用在私有写中,才能达到想要的效果
     * @throws Exception
     */
    @Test
    void getShortlytUrl() throws Exception {
        String shortlytUrl = template().getShortlytUrl(BUCKET_NAME,  "upload/2024-06-01/d18ae440132a45e5a595cebb75415e80.txt", 5);
        System.out.println("文件临时地址" + shortlytUrl);
    }
}
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Grain322

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值