如何使用Typora+MinIO+Python代码打造舒适协作环境
作者:IT王小二 时间:2023-11-12 15:12:10
不知不觉大半年没更新了...前面小二介绍过使用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
即可访问后台。
同时为了存储桶图片所有人可以访问,需要将存储桶设置为公开。
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文件位置
。
这张图片就是上传后的路径结果啦,在此,小二也祝愿大家可以愉快的写作。
来源:https://www.cnblogs.com/itwxe/p/17373790.html
标签:Typora,MinIO,Python
0
投稿
猜你喜欢
聊一聊Vue.js过渡效果
2024-05-13 09:07:51
Python中使用遍历在列表中添加字典遇到的坑
2021-12-22 13:35:32
python unittest单元测试的步骤分析
2022-03-21 05:59:10
如何给Python代码进行加密
2021-08-24 16:24:30
国内ASP开源建站系统一览
2009-07-10 13:21:00
go smtp实现邮件发送示例详解
2023-06-23 22:05:14
ASP怎么谈到应用到类的?
2008-03-10 11:21:00
用vscode开发python的步骤详解
2023-11-10 11:39:22
jquery及原生js获取select下拉框选中的值示例
2024-04-19 09:58:56
整合ckeditor+ckfinder,解决上传文件路径问题
2022-12-10 09:25:39
Oracle 数据库操作类
2009-08-12 12:06:00
简单实用的图片播放器1.0(Javascript + css )
2008-07-16 10:39:00
face++与python实现人脸识别签到(考勤)功能
2021-10-18 09:06:06
PHP之Mysql常用SQL语句示例的深入分析
2024-05-05 09:31:21
GO必知必会的常见面试题汇总
2023-07-14 01:47:07
Python信息抽取之乱码解决办法
2021-10-12 23:32:46
Python如何实现定时器功能
2023-04-13 23:19:28
python Crypto模块的安装与使用方法
2022-09-17 15:19:01
Python实现的FTP通信客户端与服务器端功能示例
2023-10-02 21:36:01
Python通过Pygame绘制移动的矩形实例代码
2023-09-05 13:28:31