zoukankan      html  css  js  c++  java
  • springboot获取七牛云空间文件列表及下载功能

    原文摘自:https://www.jiagou1216.com

    七牛云对象存储的使用方法参考:https://www.jiagou1216.com/blog/devops/339.html

    第一步:新建springboot项目,引入jar包,其中hutool-all是工具类,用来写文件下载,可以随意更换。

    <!--工具类-->
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.2.0</version>
            </dependency>
            <!--七牛云相关jar包-->
            <dependency>
                <groupId>com.qiniu</groupId>
                <artifactId>qiniu-java-sdk</artifactId>
                <version>[7.2.0, 7.2.99]</version>
            </dependency>
    
            <dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
                <version>3.14.2</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.5</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>com.qiniu</groupId>
                <artifactId>happy-dns-java</artifactId>
                <version>0.1.6</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
    

    第二步:登录七牛云,在秘钥管理中找到accessKey和secretKey

    第三步:写测试类,代码结构如下:

    package com.example.demo;
    
    import cn.hutool.core.date.DateUnit;
    import cn.hutool.core.date.DateUtil;
    import cn.hutool.core.io.FileUtil;
    import cn.hutool.core.io.StreamProgress;
    import cn.hutool.core.lang.Console;
    import cn.hutool.http.HttpUtil;
    import com.qiniu.common.Zone;
    import com.qiniu.storage.BucketManager;
    import com.qiniu.storage.Configuration;
    import com.qiniu.storage.Region;
    import com.qiniu.storage.model.FileInfo;
    import com.qiniu.util.Auth;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    
    /**
     * 架构师小跟班 www.jiagou1216.com
     * 官方API说明:https://developer.qiniu.com/kodo/sdk/1239/java#1
     */
    @SpringBootApplication
    public class DemoApplication {
        //七牛云秘钥AK
        static String accessKey = "xxxxxxxxxxxxxxxxxxxxxxx";
        //七牛云秘钥SK
        static String secretKey = "xxxxxxxxxxxxxxxxxxxxxxx";
        //七牛云空间名称
        static String bucket = "xxxxxx";
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
            list();
        }
    
        /**
         * 列表
         */
        public static void list() {
            //构造一个带指定Zone对象的配置类
            Configuration cfg = new Configuration(Region.region0());
            //...其他参数参考类注释
            Auth auth = Auth.create(accessKey, secretKey);
            BucketManager bucketManager = new BucketManager(auth, cfg);
    
            //文件名前缀
            String prefix = "";
            //每次迭代的长度限制,最大1000,推荐值 1000
            int limit = 10;
            //指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
            String delimiter = "";
    
            //列举空间文件列表
            BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);
            while (fileListIterator.hasNext()) {
                //处理获取的file list结果
                FileInfo[] items = fileListIterator.next();
                for (FileInfo item : items) {
                    download(item.key);//下载
                }
            }
        }
    
        /**
         * 下载
         */
        public static void download(String fileName) {
            String domainOfBucket = "cdn.jiagou1216.com";
            String encodedFileName = null;
            try {
                encodedFileName = URLEncoder.encode(fileName, "utf-8").replace("+", "%20");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
    
            Auth auth = Auth.create(accessKey, secretKey);
            long expireInSeconds = 3600;//1小时,可以自定义链接过期时间
            String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
            System.out.println(finalUrl);
            HttpUtil.downloadFile(finalUrl, FileUtil.file("d://七牛云"));
        }
    }
  • 相关阅读:
    年轻人的第一个 Spring Boot 应用,太爽了!
    面试问我 Java 逃逸分析,瞬间被秒杀了。。
    Spring Boot 配置文件 bootstrap vs application 到底有什么区别?
    坑爹的 Java 可变参数,把我整得够惨。。
    6月来了,Java还是第一!
    Eclipse 最常用的 10 组快捷键,个个牛逼!
    Spring Cloud Eureka 自我保护机制实战分析
    今天是 Java 诞生日,Java 24 岁了!
    厉害了,Dubbo 正式毕业!
    Spring Boot 2.1.5 正式发布,1.5.x 即将结束使命!
  • 原文地址:https://www.cnblogs.com/xyhero/p/12486064.html
Copyright © 2011-2022 走看看