Typora+MinIO+Python代码打造舒适协作环境

作者:IT王小二

博客:https://itwxe.com

不知不觉大半年没更新了...前面小二介绍过使用Typora+MinIO+Java代码打造舒适写作环境,然后有很多大佬啊,说用Java来实现简直是杀鸡用上牛刀,小二想了想,确实有点...正好小二最近在学习Python,所以咱们就改用Python实现一版。

安装MinIO

安装参考MinIO官网,或者参考小二的博客,搜索关键词 → Linux安装MinIO

安装完成之后使用域名映射好后台服务,小二使用nginx配置域名,配置参考如下。

    server {
        listen 443 ssl;
        server_name minio.itwxe.com;
        include /usr/local/nginx/conf/conf.d/common.conf;
        access_log logs/minioAccess.log;

        location / {
            proxy_pass http://127.0.0.1:9000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

    server {
        listen 443 ssl;
        server_name minio-console.itwxe.com;
        include /usr/local/nginx/conf/conf.d/common.conf;
        access_log logs/minioAccess.log;

        location / {
            proxy_pass http://127.0.0.1:9020;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

其中common.conf为域名通用配置。

        ssl_certificate /usr/local/nginx/ssl/fullchain.crt;
        ssl_certificate_key /usr/local/nginx/ssl/itwxe.com.key;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout 30m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!MD5:!EXPORT56:!EXP;
        ssl_prefer_server_ciphers on;
        proxy_connect_timeout 500;
        proxy_send_timeout 500;
        proxy_read_timeout 500;
        client_max_body_size 100m;

配置好之后访问https://minio-console.itwxe.com即可访问后台。

同时为了存储桶图片所有人可以访问,需要将存储桶设置为公开。

设置存储桶为public

Python代码实现上传

首先,小二作为一个半吊子Python学习者,看了下MinIO官网提供的SDK范例。

安装依赖。

pip install minio

官网示例代码。

from minio import Minio
from minio.error import S3Error


def main():
    # Create a client with the MinIO server playground, its access key
    # and secret key.
    client = Minio(
        "play.min.io",
        access_key="Q3AM3UQ867SPQQA43P2F",
        secret_key="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
    )

    # Make 'asiatrip' bucket if not exist.
    found = client.bucket_exists("asiatrip")
    if not found:
        client.make_bucket("asiatrip")
    else:
        print("Bucket 'asiatrip' already exists")

    # Upload '/home/user/Photos/asiaphotos.zip' as object name
    # 'asiaphotos-2015.zip' to bucket 'asiatrip'.
    client.fput_object(
        "asiatrip", "asiaphotos-2015.zip", "/home/user/Photos/asiaphotos.zip",
    )
    print(
        "'/home/user/Photos/asiaphotos.zip' is successfully uploaded as "
        "object 'asiaphotos-2015.zip' to bucket 'asiatrip'."
    )


if __name__ == "__main__":
    try:
        main()
    except S3Error as exc:
        print("error occurred.", exc)

于是乎,小二开始依葫芦画瓢,代码如下。

import sys
import time
from pathlib import Path
import random
from minio import Minio

minio_domain = 'minio.itwxe.com'
minio_access_key = '你的账号名称'
minio_secret_key = '你的密码'
# 存储桶名称
bucket_name = 'img'
# 存储桶子文件夹名称
bucket_name_dir_name = 'blog'

# 获取minio客户端连接
minio_client = Minio(minio_domain, minio_access_key, minio_secret_key)

if not minio_client.bucket_exists(bucket_name):
    # 如果存储桶不存在,则创建
    minio_client.make_bucket(bucket_name)

# 获取图片参数
images = sys.argv[1:]

for image in images:
    # 文件后缀
    suffix = Path(image).suffix
    # 自定义文件名,使用13位时间戳+2位随机数
    file_name = '{}{}{}'.format(round(time.time() * 1000), random.randint(10, 99), suffix)
    # 文件存储桶下子路径拼接
    remote_full_path_name = '{}/{}'.format(bucket_name_dir_name, file_name)
    # 上传文件
    minio_client.fput_object(bucket_name, remote_full_path_name, image)
    # 打印文件路径
    print("https://minio.itwxe.com/{}/{}".format(bucket_name, remote_full_path_name))

写完之后小二不禁感慨,确实比Java简单亿点点,设置一下typora,python 你的python文件位置

image-20230504223443595

这张图片就是上传后的路径结果啦,在此,小二也祝愿大家可以愉快的写作。

posted @ 2023-05-05 12:31  IT王小二  阅读(656)  评论(0编辑  收藏  举报