整合对象存储工具类,可以直接使用

aliyun aliyunHeaper

package com.leyongleshi.idea.plugin.pasteimageintomarkdown;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

<!--more-->

/**
 * @author cl
 * @date 2020/1/16
 * @desc
 */
public class AliyunOSSHelper {

    private String bucketName;

    private OSSClient ossClient;

    public AliyunOSSHelper(String endpoint, String accessKeyId, String accessKeySecret, String bucketName) {
        this.bucketName = bucketName;
        this.ossClient = (OSSClient) new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    public String upload(InputStream ins, String filePathName) {
        if (filePathName.startsWith("/")) {
            filePathName = filePathName.substring(1);
        }
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, filePathName, ins);
        ossClient.putObject(putObjectRequest);
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100);
        URL url = ossClient.generatePresignedUrl(bucketName, filePathName, expiration);
        return url.toString();
    }

    public String upload(File file, String filePathName) {
        try {
            return upload(new FileInputStream(file), filePathName);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public String upload(BufferedImage image, String filePathName) {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "PNG", result);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return upload(new ByteArrayInputStream(result.toByteArray()), filePathName);
    }

}

qiniu

package com.leyongleshi.idea.plugin.pasteimageintomarkdown;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Region;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author cl
 * @date 12/09/2017
 * @desc
 */
public class QiniuHelper {

    private static final UploadManager UPLOAD_MANAGER = new UploadManager(new Configuration());
    private String accessKey;
    private String secretKey;
    private String bucket;
    private String cndDomain;
    private int uploadRetryTimes = 3;

    public QiniuHelper(String accessKey, String secretKey, String bucket, String cndDomain) {
        this(accessKey, secretKey, bucket, cndDomain, 3);
    }

    public QiniuHelper(String accessKey, String secretKey, String bucket, String cndDomain, int uploadRetryTimes) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.bucket = bucket;
        this.cndDomain = cndDomain;
        if (uploadRetryTimes <= 0) {
            uploadRetryTimes = 3;
        } else if (uploadRetryTimes > 20) {
            uploadRetryTimes = 20;
        }
        this.uploadRetryTimes = uploadRetryTimes;
    }

    public static String upToken(String accessKey, String secretKey, String bucket) {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        return upToken;
    }

    public String upToken() {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        return upToken;
    }

    public String upToken(Long ttlSecond) {
        Auth auth = Auth.create(accessKey, secretKey);
        if (ttlSecond == null || ttlSecond <= 1) {
            return upToken();
        }
        String upToken = auth.uploadToken(bucket, (String) null, ttlSecond, (StringMap) null, true);
        return upToken;
    }

    public String upToken(String key, long expires, StringMap policy, boolean strict) {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket, key, expires, policy, strict);
        return upToken;
    }

    public String upload(File file, String fileName) {
        RuntimeException ex = new RuntimeException("七牛图片上传失败[未知错误]");
        for (int i = 0; i < uploadRetryTimes; i++) {
            String token = upToken();
            try {
                Response response = UPLOAD_MANAGER.put(file, fileName, token);
                if (response.isOK()) {
                    return cndDomain + fileName;
                }
                ex = new RuntimeException("七牛图片上传失败[" + response.bodyString() + "]");
            } catch (QiniuException e) {
                ex = new RuntimeException("七牛图片上传失败", e);
            }
        }
        throw ex;
    }

    public String upload(byte[] data, String fileName) {
        RuntimeException ex = new RuntimeException("七牛图片上传失败[未知错误]");
        for (int i = 0; i < uploadRetryTimes; i++) {
            String token = upToken();
            try {
                Response response = UPLOAD_MANAGER.put(data, fileName, token);
                if (response.isOK()) {
                    return cndDomain + fileName;
                }
                ex = new RuntimeException("七牛图片上传失败[" + response.bodyString() + "]");

            } catch (QiniuException e) {
                ex = new RuntimeException("七牛图片上传失败", e);
            }
        }
        throw ex;
    }

    public String upload(BufferedImage img, String fileName) {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try {
            ImageIO.write(img, "PNG", result);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return upload(result.toByteArray(), fileName);
    }

    public String upload(InputStream fileIs, String fileName) {
        RuntimeException ex = new RuntimeException("七牛图片上传失败[未知错误]");
        for (int i = 0; i < uploadRetryTimes; i++) {
            String token = upToken();
            try {
                Response response = UPLOAD_MANAGER.put(fileIs, fileName, token, null, null);
                if (response.isOK()) {
                    return cndDomain + fileName;
                }
                ex = new RuntimeException("七牛图片上传失败[" + response.bodyString() + "]");

            } catch (QiniuException e) {
                ex = new RuntimeException("七牛图片上传失败", e);
            }
        }
        throw ex;
    }
}

腾讯云

package com.leyongleshi.idea.plugin.pasteimageintomarkdown;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.Date;

/**
 * @author cayden
 * @date 2020/1/18 13:37
 */
public class TencentOSSHelper {

    private String bucketName;

    private COSClient cosClient;

    public TencentOSSHelper(String secretId, String secretKey, String region, String bucketName){
        COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        this.cosClient = new COSClient(cred, clientConfig);
        this.bucketName = bucketName;
    }

    public String upload(InputStream inputStream, String filePathName){
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, filePathName, inputStream, new ObjectMetadata());
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100);
        URL url = cosClient.generatePresignedUrl(bucketName,filePathName, expiration);
        //组装url,经过测试,与阿里云不同,腾讯云不能通过url.toString()获得url,但是能够通过规则http:// + 图片地址 + / + 地址获得url
        String uriOutput = url.getProtocol() + "://" + url.getHost() + "/"+ filePathName;
        return uriOutput;
    }

    public String upload(File file, String filePathName) {
        try {
            return upload(new FileInputStream(file), filePathName);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public String upload(BufferedImage image, String filePathName) {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "PNG", result);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return upload(new ByteArrayInputStream(result.toByteArray()), filePathName);
    }
}

欢迎使用idea 插件PasteImagesIntoMarkdown github: https://github.com/rocky-peng/PasteImageToMarkdown

[图片上传失败...(image-66fd47-1611643004990)]

后续将更新更多图床服务工具类

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容