0%

利用文件服务器搭建私人图床

常见的利用github或者gitee搭建图床方式,总会遇到一些奇怪的原因导致无法使用,比如gitee的防盗链,github科学上网问题。我理解的图床就是一个简单的文件服务器,只是这个文件服务器只存储图片而已,有了这个认识,我们可以利用闲置的服务器(云服务器或者自家电脑内网穿透方式)来搭建一个文件服务器。步骤如下:

  1. 搭建文件服务器

    该工具目前为linux 版本,windows无法使用

    • 下载文件服务器工具:链接: https://pan.baidu.com/s/14EIL3ryx45OApcMV7S6LnQ 提取码: vuny

    • 修改配置文件

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      #服务监听店端口
      port=8080
      #上传文件存放路径
      uploadDir=/home/share
      #上传 http请求路由
      route=/upload
      #是否token开启验证
      auth=false
      #验证token值,与客户端上传比对
      token=helloworld
      #end
    • 启动服务

      1
      ./CloudDiskWeb config.ini

      访问图片url:http://host:8080/xxx.png

      上传路由为:http://host:8080/upload

    1. 编写自动上传文件脚本 upload.py

      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
      import requests
      import sys

      upload_url = 'http://132.232.11.158:5008/upload'


      def upload_images(images, dir='/sunshine'):
      for img in images:
      file_name = img.split('/')[-1]
      files = [
      ('file', (file_name, open(img, 'rb'), 'image/png'))
      ]

      response = requests.request("POST", upload_url + dir, files=files)
      if response.status_code == 200:
      path = f'{dir}/{file_name}'
      else:
      path = ""
      print(f"{upload_url.strip('/upload')}{path}") # 一定要输出文件路径,不然typora无法获取到新图片的url


      if __name__ == '__main__':
      images = sys.argv[1:]
      upload_images(images)

    2. typora配置

      文件->偏好设置->图像->上传服务 选择自定义命令,在命令中输入python %home/upload.py

      image-20230315152638971

      到此,所有配置完成。

      粘贴图片到typora后,自动上传并显示新的图片url。