python调用MinIO对象存储

  sre

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("这里写你的Minio地址,格式为域名:端口,不带http://",
                    access_key='你的Minio账号', secret_key='你的Minio密码', secure=False)//secure为True的话第一项会填充为https://
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)//先把网图下载到本地了,然后在传到Minio
                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:
        minioClient.fput_object(bucket_name='blog', object_name=date + "/" + new_file_name, file_path=image)
        if image.endswith("temp"):
            os.remove(image)
        result = result +"你的Nginx地址,比如http://yourdomain:8081" + "/blog/" + date + "/" + new_file_name + "\n"
    except ResponseError as err:
        result = result + "error:" + err.message + "\n"
print(result)

LEAVE A COMMENT

Captcha Code