由于本博客部署在阿里云docker中,上次漏洞事件导致ecs被挖矿,重启应用的时候未保存图片,所以前期上传的图片都丢失了。偶尔看到七牛云提供免费OSS存储,在此接入OSS存储。

1、在七牛云申请OSS,获取一个bucket。这里是huxdiy

2、应用程序配置文件新增七牛云配置:

#  七牛秘钥
oss.qiniu.accessKey: ***************************************
oss.qiniu.secretKey: ***************************************
#存储空间bucket名字
oss.qiniu.bucketPictureName: huxdiy
#图片外部访问拦截
oss.qiniu.domainPicture: http://scnpizocy.hn-bkt.clouddn.com/

3、引用七牛云SDK

        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>7.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>

4、初始化SDK

@Component
@ConfigurationProperties(prefix = "oss.qiniu")
public class InitConfig {
    /**
     * AccessKey
     */
    private String accessKey;
    /**
     * SecretKey
     */
    private String secretKey;
    /**
     * 图片存储空间名
     */
    private String bucketPictureName;
    /**
     * 图片外链
     */
    private String domainPicture;
    /**
     * 文件存储空间名
     */
    private String bucketFileName;
    /**
     * 文件外链
     */
    private String domainFile;

    @Bean
    public void init() {
        Constant.accessKey = this.accessKey;
        Constant.secretKey = this.secretKey;
        Constant.bucketPictureName = this.bucketPictureName;
        Constant.domainPicture = this.domainPicture;
        Constant.bucketFileName = this.bucketFileName;
        Constant.domainFile = this.domainFile;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getBucketPictureName() {
        return bucketPictureName;
    }

    public void setBucketPictureName(String bucketPictureName) {
        this.bucketPictureName = bucketPictureName;
    }

    public String getDomainPicture() {
        return domainPicture;
    }

    public void setDomainPicture(String domainPicture) {
        this.domainPicture = domainPicture;
    }

    public String getBucketFileName() {
        return bucketFileName;
    }

    public void setBucketFileName(String bucketFileName) {
        this.bucketFileName = bucketFileName;
    }

    public String getDomainFile() {
        return domainFile;
    }

    public void setDomainFile(String domainFile) {
        this.domainFile = domainFile;
    }
}

5、编写上传工具类(这里可以图片,文件分开上传,可以自定义访问文件名称),输入流上传成功,返回一个url提供七牛云访问。

public class QiNiuUtil {

    /**
     * 将图片上传到七牛云
     */
    public String upload(FileInputStream file, String fileType) throws Exception {
        // zone0华东区域,zone1是华北区域,zone2是华南区域
        Configuration cfg = new Configuration(Region.region2());
        UploadManager uploadManager = new UploadManager(cfg);
        // 生成上传凭证,然后准备上传
        Auth auth = Auth.create(Constant.accessKey, Constant.secretKey);
        String upToken = null;
        String path = null;
        if (fileType.equals(Constant.IMAGE)) {
            upToken = auth.uploadToken(Constant.bucketPictureName);
            path = Constant.domainPicture;
        } else if (fileType.equals(Constant.FILE)) {
            upToken = auth.uploadToken(Constant.bucketFileName);
            path = Constant.domainFile;
        }
        // 第二个参数是soft123上传文件目录,和访问文件名称
        Response response = uploadManager.put(file, "soft123/"+getFileName(), upToken, null, null);
        // 解析上传成功的结果
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        return path + putRet.key;
    }

    private String getFileName(){
        //生成文件名称通用方法
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        Random r = new Random();
        StringBuilder tempName = new StringBuilder();
        tempName.append(sdf.format(new Date())).append(r.nextInt(100)).append("pic");
        String newFileName = tempName.toString();
        return newFileName;
    }

6、如果配置了自定义域名,也可以通过域名访问。上传文件如下,已经上传到指定文件夹中