(上传代码使用Java编写,因此需要JDK+Maven环境。没有的话进来看看也行,使用其他编程语言原理一样)
作为Markdown的忠实用户,Typora哪哪都好,就是图片存储有点难顶,存本地容易丢,存云端太麻烦。
然后就有了接下来的操作
- 花十块钱买了阿里云OSS 40G 1年 的存储空间
- 写几段代码
- 修改下 Typora 配置
真香!(禁止套娃)
购买阿里云的OSS存储
这步除了钱以外,应该没有啥问题。依据官方文档走到如下步骤,自行创建个 Bucket,然后再继续下一步。
简单堆个码
由于需要使用阿里云为我们提供的OSS依赖,这里简单构建一个Maven一个程序,pom和类已经摆出来,要是连maven也不熟悉的-----(好好学习😁,没其他的招儿)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.swing</groupId>
<artifactId>oss</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.swing.oss.Upload</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Upload.java
package com.swing.oss;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author swing
*/
public class Upload {
/**
* 上传文件至阿里云
*
* @param bytes 文件字节数组
* @param extName 文件扩展名
* @return 文件下载路径
*/
public static String uploadFile(byte[] bytes, String extName) {
//修改为你的 endpoint
String endpoint = "xxxxxx";
//修改为你的 accessKeyId
String accessKeyId = "xxxxxx";
//修改为你的 accessKeySecret
String accessKeySecret = "xxxxxx";
//修改为你的 bucketName
String bucketName = "swing-doc";
String objectName = getObjectName(extName);
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(bytes));
ossClient.putObject(putObjectRequest);
ossClient.shutdown();
//修改为你的路径
return "https://swing-doc.oss-cn-beijing.aliyuncs.com/" + objectName;
}
public static void main(String[] args) throws IOException {
String fileName = args[0];
//获取后缀
String extName = fileName.split("\\.")[1];
File file = new File(fileName);
FileInputStream fileInputStream = new FileInputStream(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024 * 1024 * 10];
int n = 0;
while ((n = fileInputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, n);
}
byte[] fileBytes = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
fileInputStream.close();
System.out.println(uploadFile(fileBytes, extName));
}
/**
* 获取唯一的文件名
*
* @param extName 文件扩展名
* @return 唯一文件名
*/
public static String getObjectName(String extName) {
//使用时间戳作为文件名
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss-SSS");
String format = simpleDateFormat.format(new Date());
return "markdown/" + format + "." + extName;
}
}
进入到项目目录下:
# 打包
mvn clean package -Dmaven.test.skip=true
简单传个图片验证一下
# 注意修改为自己的地址,别都复制
E:\swing\desktop>java -jar E:\swing\desktop\oss\target\oss-1.0.0-SNAPSHOT-jar-with-dependencies.jar "E:\swing\picture\aima.png"
https://swing-doc.oss-cn-beijing.aliyuncs.com/markdown/2020-09-23-09-26-30-500.png
成功返回上传后的文件地址
Typora配置修改
打开 【文件】》【偏好设置】》【图像】
Typora上传文件的原理也很简单,就是 脚本+“文件路径”,使用Java捕获这个文件地址,转化为 byte[] 然后上传到OSS,最后返回给它存储地址即可。
如上配置后,点击【验证图片上传选项】
大功告成!
总结一下
使用体验:真香!
不足之处:对于已删除的文件,远程服务器仍然会保留,时间久了,会有一些无用的数据。由于Typora删除图片时未提供任何触发器,因此这个问题还不知怎么解决,如有大佬知道,还望指点🏃♂️
欢迎 点赞👍 留言 关注 收藏🎉 !!!