专业的编程技术博客社区

网站首页 > 博客文章 正文

免费图床不稳定!收费的又觉肉痛,2分钟Minio快速构建图床

baijin 2024-09-27 06:49:15 博客文章 7 ℃ 0 评论

开头说几句

  1. 小编写博客断断续续也有好几年了,之前一直都是使用iPic的免费图床。
  2. 但是后来发现了一个严重的问题,这个图床时间长了图片会失效啊!!!
  3. 简直是“丧尽天良”,导致我之前的博客图片全都访问不了了,光看文字绝对是一脸懵!
  4. 最近发现了开源的对象存储Minio,所以痛定思痛决心要搞一个自己的图床。

详读该文的资源需求

  1. 我们说了要自己搞一个图床了,那我们就至少需要有一台服务器来运行我们的Minio
  2. 但是~既然我们前几文中都说了Kubernetes,那我们今天就来使用K8S来构建我们的Minio应用。
  3. 既然如此我们就需要有一套K8s的环境了。
  4. 上述都准备好之后,我们还需要有个公网IP来为我们提供穿透服务,使我们的图片可以在外网访问。
  5. 最后就是我们使用的Markdown工具Typora,这个工具用起来简直得心应手,主要是支持自定义图片上传功能,非常适合我们自行构建图床。
  6. 还没有学习过K8S、内网穿透的小伙伴们,可以跳转小编主页查看顶置哦,私"K8s"即可获取相关下载地址。
  7. 内网穿透在家远程连接公司电脑?我是被逼的
  8. 应用版本多,环境乱七八糟?利用kubeadm快速构建k8s-1.18.5环境 终端管理K8s命令太多太费劲?试试官方的Dashboard,你100%喜欢

如何使用Kubernetes运行Minio?

  1. 首先我们先去Minio官方文档查看一下K8S的启动方式。
  1. 看到这个页面啦,我们只要输入相关的信息,他就可以生成一个yaml文件供我们创建容器。
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  # This name uniquely identifies the PVC. Will be used in deployment below.
  name: minio-pv-claim
  labels:
    app: minio-storage-claim
spec:
  # Read more about access modes here: https://kubernetes.io/docs/user-guide/persistent-volumes/#access-modes
  accessModes:
    - ReadWriteOnce
  resources:
    # This is the request for storage. Should be available in the cluster.
    requests:
      storage: 200Gi
  # Uncomment and add storageClass specific to your requirements below. Read more https://kubernetes.io/docs/concepts/storage/persistent-volumes/#class-1
  #storageClassName:
---
apiVersion: extensions/v1
kind: Deployment
metadata:
  # This name uniquely identifies the Deployment
  name: minio-deployment
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        # Label is used as selector in the service.
        app: minio
    spec:
      # Refer to the PVC created earlier
      volumes:
      - name: storage
        persistentVolumeClaim:
          # Name of the PVC created earlier
          claimName: minio-pv-claim
      containers:
      - name: minio
        # Pulls the default MinIO image from Docker Hub
        image: minio/minio
        args:
        - server
        - /storage
        env:
        # MinIO access key and secret key
        - name: MINIO_ACCESS_KEY
          value: "leiyuan"
        - name: MINIO_SECRET_KEY
          value: "Leiyuan123."
        ports:
        - containerPort: 9000
        # Mount the volume into the pod
        volumeMounts:
        - name: storage # must match the volume name, above
          mountPath: "/storage"
---
apiVersion: v1
kind: Service
metadata:
  name: minio-service
spec:
  type: LoadBalancer
  ports:
    - port: 9000
      targetPort: 9000
      protocol: TCP
  selector:
    app: minio
  1. 他这里的Service类型为LoadBalancer,如果有必要我们可以根据自身的需求更改类型NodePort
apiVersion: v1
kind: Service
metadata:
  name: minio-service
spec:
  type: NodePort
  ports:
    - port: 9000
  selector:
    app: minio
  1. 使用该配置文件创建Minio应用$ kubectl create -f minio-deployment.yaml或者直接使用Dashboard创建应用。
  1. 我们在service中并没有指定NodePort端口,需要创建后查看一下对外的映射端口
  1. 接下来就是访问我们的Minio了,ip:port即可访问
  1. 这里输入我们设置的账号密码即可正常登陆。至此我们的Minio算是搭建完毕,怎么样是不是很简单?

如何将Minio穿透至外网可访问?

这里我就不做详细的介绍啦,大家可以查看我的这篇文章,相信你可以很快搞定穿透的问题。

如何在Typora中实现Minio的无缝上传?

  1. 该软件本是不支持Minio图床的,但是他有个很厉害的功能。你可以自定义命令来实现图片上传的功能。
  2. 那我们就使用python脚本来实现图片的上传功能,然后在Typora中使用python脚本进行无缝图片上传。
  3. 首先我们需要安装Python,安装过程不再赘述,我这里选择使用python3。
  4. 其次我们需要安装Minio相关的python库以支持我们的脚本运行。
# 获取官方代码
$ git clone https://github.com/minio/minio-py
# 进行安装
$ cd minio-py
$ sudo python3 setup.py install
  1. 在安装的过程中可能会提示缺少某些库,那就需要小伙伴们看一下缺什么安装什么啦
  2. 编写上传脚本
import os
import time
import uuid
import sys
import requests
from minio import Minio
from minio.error import ResponseError
import warnings
warnings.filterwarnings('ignore')
images = sys.argv[1:]
# 上传的地址以及用户名密码
minioClient = Minio("{ip:port}",
                    access_key='leiyuan', secret_key='Leiyuan123.', secure=False)
result = "Upload Success:\n"
date = time.strftime("%Y%m", time.localtime())
def download(image_url):
    local_path = os.getcwd() + "/temp"
    r = requests.get(image_url, verify=False)
    with open(local_path, "wb") as code:
        code.write(r.content)
    return local_path
for image in images:
    if os.path.isfile(image):
        file_type = os.path.splitext(image)[-1]
        new_file_name = str(uuid.uuid1()).replace('-', '') + file_type
    elif image.startswith("https://") or image.startswith("http://"):
        if image.endswith(".png") or image.endswith(".jpg") or image.endswith(".jpeg") or image.endswith(".gif"):
            url = image.split("/")
            if len(url) > 1:
                image = download(image)
                new_file_name = url[-1]
            else:
                result = result + "error:parsing image error!"
                continue
        else:
            result = result + "error:parsing image error!"
            continue
    else:
        result = result + "error:parsing image error!"
        continue
    try:
      # blog为桶名称,可自行修改
        minioClient.fput_object(bucket_name='blog', object_name=date + "/" + new_file_name, file_path=image)
        if image.endswith("temp"):
            os.remove(image)
            # 外网的IP以及端口号
        result = result +"http(s)://ip:port" + "/blog/" + date + "/" + new_file_name + "\n"
    except ResponseError as err:
        result = result + "error:" + err.message + "\n"
print(result)
  1. 这里大家要注意替换注释处的参数信息
  2. 这样就可以了,我们来运行一下试试
  1. 可以看到我们上传成功了并且他给我们返回了一个下载地址,接下来我们只需要把脚本集成到Typora软件让它实现自动上传即可

配置Typora上传设置

  1. 打开偏好设置中的图像设置
  2. 配置如下
  1. 在自定义命令中填入我们的上传命令:
$ /Library/Frameworks/Python.framework/Versions/3.6/bin/python3 /Users/leiyuan/Documents/今日头条文章/upload_minio.py
  1. 这里注意一下我们都是用绝对路径避免它找不到命令或者脚本。
  2. 这样设置之后就可以了,我们打开Typora然后将图片拖拽至编辑框即可实现自动上传。

结束了~~

  1. 至此我们的图床算是搞定了,只要你的minio服务不坏,数据不丢,那就没问题了,你的图片会一直都在。
  2. 妈妈再也不用担心免费图床害我文章阅读不通了~~~
  3. 大家有什么不懂得都可以直接来问我,及时解答。

最后的最后

  1. 没有了!再见吧!
  2. 别走啊~持续关注持续输出,感谢大家!!!

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表