一. 介绍

​ 本章主要讲述的是通过Typora + Python 脚本来实现Typora 工具编辑Markdown文件时的图片上传处理。通过Docker部署Nginx,MinIO容器,Nginx代理访问MinIO资源。

效果如下:

1

二. 相关脚本

​ 以下是相关服务及脚本实例。

1. Python3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------
# -- OSS文件上传
# --
# ****************************
# Author: lmay.Zhou
# Blog: www.lmaye.com
# Date: 2021/1/21 11:15
# Email lmay@lmaye.com
# ----------------------------------------------------------
import sys
import uuid
from pathlib import Path
from minio import Minio

# access_key: MinIo帐号
# secret_key: MinIo密码
minio_storage = Minio("192.168.30.180:9000", access_key='admin', secret_key='YouGuess', secure=False)
images = sys.argv[1:]
for image in images:
print("File Uploading ...")
suffix = Path(image).suffix
file_name = str(uuid.uuid4()) + suffix
# 存储桶名称
bucket_name = "hexo-blog"
if not minio_storage.bucket_exists(bucket_name):
# 如果存储桶不存在,则创建
minio_storage.make_bucket(bucket_name)
minio_storage.fput_object(bucket_name, file_name, image, content_type="image/png", part_size=10485760)
print("https://www.lmaye.com/files/{}/{}".format(bucket_name, file_name))

2. Nginx 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#user  nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

#location / {
# root html;
# index index.html index.htm;
#}

#location / {
# proxy_pass http://spring-boot-elasticsearch:60;
# proxy_set_header Host $host;
# # proxy_set_header X-Forwarded-Host $server_name;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}

# 配置MinIO资源
# https://www.lmaye.com/files/hexo-blog/287872bb-f461-4abe-8ce0-aaac09db1633.png
location ^~ /files/ {
root /usr/share/nginx/html;
}
}
}

3. Docker Compose

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
version: "3"
services:
nginx:
container_name: blog-nginx
image: nginx:1.16
restart: on-failure
ports:
- 80:80
- 443:443
volumes:
- ./nginx/www:/usr/share/nginx/html
- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/logs:/var/log/nginx

minio:
# 容器名称
container_name: blog-minio
# 镜像:版本
image: minio/minio:latest
# 重启机制
# -- no: 禁止自动重启容器(默认);
# -- always: 无论如何容器都会重启;
# -- on-failure: 当出现on-failure报错时, 容器重新启动;
restart: on-failure
command: server /data
# 端口
ports:
- 9000:9000
environment:
MINIO_ACCESS_KEY: admin
MINIO_SECRET_KEY: YouGuess
# 持久化挂载
volumes:
# Nginx代理访问资源
- ./nginx/www/files:/data
- ./minio/conf:/root/.minio

三. Typora配置

1. Python脚本测试:

​ CMD命令:

1
2
3
E:\md>python oss_upload.py "C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210121112644841.png"
File Uploading ...
https://www.lmaye.com/files/hexo-blog/7dd6ea8a-551c-48dc-baba-e3506e52556a.gif

2. Typora工具配置

配置步骤:文件 -> 偏好设置 -> 图像

如下所示

命令:python oss_upload.py(Python脚本没有写觉得路径,因为我把脚本放在了MD文件目录下)

image-20210121152155883

最后更新: 2021年04月01日 11:03

原始链接: https://www.lmaye.com/2021/01/24/20210124003256/

× 多少都行~
打赏二维码