Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

Typora image upload plug-in based on MiniO storage engine using Java

Last article we introduced the daily operation and maintenance of MINIO, today we are based on miniO Java SDK handlift a Typora upload plug-in, this step is based on the previous introduction, do not know you can click the plane to view.

For those of you who don’t know what Typora is, wall Crack Amway is giving you Typora a Markdown tool. Markdown document writing artifact, simple, convenient, free; What you see is what you get, without the need for a split screen preview or a new page preview.

Why

Google Typora and you’ll find lots of articles using Picgo/Picgo-core. Why not use it?

  • Configure trival
  • Configure trival
  • Configure trival
  • Show off

Directions for use

Installing a plug-in

Assuming you have the Java environment installed.

Download the code, compile the source code, or simply use the compiled JAR package, put the JAR package in a fixed directory, and create the conf.properties file in the same directory as the JAR package. The following

# Upload domain name
host=http://127.0.0.1:9000
# MINIO_ACCESS_KEY
accessKey=ProEXEC
# MINIO_SECRET_KEY
secretKey=ONRPROEXEC
The bucket for storing files does not exist and is created automatically
bucketName=testbucket
# file prefix, used to distinguish files, can be unmatched
customKey=javadev
Copy the code

The plug-in configuration

windows

File -> Preferences -> Image

mac

Example Set a custom command

Enter java-jar /{yourDir}/ typora-minio-1.0-snapshot-jar-with-dependencies. Jar in the command bar

Click verify image upload option and the picture below appears indicating that the image has been uploaded successfully

Insert pictures can be directly uploaded automatically

The code on

POM depends on

In order to facilitate the operation, we use the form of FatJAR for packaging, and the key code is as follows

Dependency configuration:

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.3.1</version>
</dependency>
Copy the code

Compile and configure:

<build>
    <plugins>
        <! -- Apache Maven JAR Plugin This plugin provides the capability to build jars. -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <archive>
                    <addMavenDescriptor>false</addMavenDescriptor>
                </archive>
            </configuration>
        </plugin>
        <! -- Apache Maven Assembly Plugin The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive. -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>false</addClasspath>
                    </manifest>
                    <manifestEntries>
                        <Main-Class>top.javadev.typora.minio.App</Main-Class>
                        <Can-Redefine-Classes>true</Can-Redefine-Classes>
                        <Built-By>javadev.top</Built-By>
                        <Created-By>javadev.top</Created-By>
                    </manifestEntries>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Copy the code

Key Java code

package top.javadev.typora.minio;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;
import top.javadev.typora.minio.config.UploadConfig;
import top.javadev.typora.minio.util.Utils;

/ * * *@author javadev.top
 * @date2021/10/11 * /
public class App {

    public static void main(String[] args) {
        if (args.length > 0) {

            App app = new App();
            UploadConfig config = Utils.loadConfig();
            for (String path : args) {
                String url = app.upload(path, config);
                // Typora will extract the output of the script as the address, replacing the image link on Markdown
                System.out.println(config.getHost() + "/" + config.getBucketName() + "/"+ url); }}else{}}/** * upload **@paramPath * File path *@paramConfig * Configuration information *@return {@link String}
     */
    public String upload(String path, UploadConfig config) {
        StringBuilder url = new StringBuilder();
        try {
            MinioClient minioClient = MinioClient.builder().endpoint(config.getHost())
                .credentials(config.getAccessKey(), config.getSecretKey()).build();

            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(config.getBucketName()).build());
            if(! found) { minioClient.makeBucket(MakeBucketArgs.builder().bucket(config.getBucketName()).build()); }if(config.getCustomKey() ! =null && config.getCustomKey().trim().length() > 0) {
                url.append(config.getCustomKey()).append("/");
            }
            url.append(System.nanoTime());
            url.append(Utils.getSuffix(path));
            minioClient.uploadObject(UploadObjectArgs.builder().bucket(config.getBucketName()).object(url.toString())
                .filename(path).build());

        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
            System.out.println("HTTP trace: " + e.httpTrace());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        returnurl.toString(); }}Copy the code

So much for using Minio.

Complete code download