整合對(duì)象存儲(chǔ)工具類,可以直接使用

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("七牛圖片上傳失敗[未知錯(cuò)誤]");
        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("七牛圖片上傳失敗[未知錯(cuò)誤]");
        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("七牛圖片上傳失敗[未知錯(cuò)誤]");
        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,經(jīng)過測試,與阿里云不同,騰訊云不能通過url.toString()獲得url,但是能夠通過規(guī)則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)]

后續(xù)將更新更多圖床服務(wù)工具類

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容