minio 前端页面下载带中文名称的文件,文件名变成乱码了怎么办?

我用 docker 跑了一个 minio

version: "3"
services:
    public_minio:
      container_name: public_minio
      image: minio/minio
      ports:
        - "9000:9000" # client port
        - "9002:9002" # console port
      command: server /data --console-address ":9002" #指定容器中的目录 /data
      environment:
        MINIO_ACCESS_KEY: ponponon #管理后台用户名
        MINIO_SECRET_KEY: ponponon #管理后台密码,最小8个字符

图片.png

minio 的前端页面显示正常的,但是下载到本地的文件,变成蝌蚪文了

图片.png

上传文件,我不是通过前端上传的,是写了一段 python 代码

import os
from loguru import logger
from pathlib import Path
from mark import BASE_DIR
import multiprocessing
import itertools
from typing import Generator, Callable
from concurrent.futures.thread import ThreadPoolExecutor


def upload(snapshot_filename: Path):
    try:
        from core.minio.crud import exist, upload, upload_file
        # print(str(snapshot_filename))

        _key = str(snapshot_filename).removeprefix('resource/')
        if not exist(_key):
            # with open(snapshot_filename, 'rb') as f:
            upload_file(snapshot_filename, _key)
    except Exception as error:
        logger.exception(error)
        # os.remove(image)


def process_func(snapshot_filenames: list[Path]):
    try:

        pool = ThreadPoolExecutor(max_workers=1)

        assert isinstance(snapshot_filenames, list)
        assert isinstance(snapshot_filenames[0], Path)

        for snapshot_filename in snapshot_filenames:
            pool.submit(upload, snapshot_filename)

        pool.shutdown(wait=True)
    except Exception as error:
        logger.exception(error)


def gen(snapshot_filenames: list[Path]):
    for snapshot_filename in snapshot_filenames:
        yield snapshot_filename


if __name__ == "__main__":
    snapshot_dir = Path(
        'resource/video')

    snapshot_filenames: list[Path] = []

    for item in os.walk(snapshot_dir):
        dirpath, dirnames, filenames = item
        dirpath: str
        dirnames: list[str]
        filenames: list[str]

        for filename in filenames:
            snapshot_filenames.append(Path(dirpath)/filename)

    snapshot_filenames = [
        i for i in snapshot_filenames]

    snapshot_filenames.sort()

    logger.debug(f'一共有 {len(snapshot_filenames)} 个文件')

    pool = multiprocessing.Pool(processes=1)

    # snapshot_filenames = snapshot_filenames[:1]

    logger.debug(f'snapshot_filenames len: {len(snapshot_filenames)}')

    gg = gen(snapshot_filenames)

    for _snapshot_filenames in iter(
        lambda: list(itertools.islice(gg, 1000)),
        []
    ):
        pool.apply_async(process_func, (_snapshot_filenames,))
    logger.debug(f'投递完成')
    pool.close()
    pool.join()

阅读 5.1k
1 个回答

这个问题,我写了一个 python 脚本复现

随便上传一个文件名中带有中文的文件

图片.png

import requests

url = "http://192.168.38.191:9002/api/v1/buckets/image-search-engine/objects/download?prefix=5Lul5Zu%205pCc5Zu%20LnBkZg%3D%3D"


headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/115.0",
    "Accept": "*/*",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate",
    "Referer": "http://192.168.38.191:9002/buckets/image-search-engine/browse",
    "Cookie": "sidebar_collapsed=false; token=AOzv/BU/U2Jm1UOXXVKK+Blh5W4cwWVnH/k2qFAov6/H6Lzif/aS8Fe6dSUgrxaed1mN3uywqxsEd2SADqROyb0wiKnvZmlZT5V0ys1k+LkVXecAGMEzGbLh4DfzbRNX8OmZDRmhSTqxqKfFyDU6WPeiEj7Q/OywrPxPEl/71TGLexLzTDLERzhVzGlWm5mCCBLr4BIHLVx4AV9zvWVUh9UdAcu0aYdKG9rVAUgEKAsPNfIEaMygQbFEcj0IW50WaST/ogSsP1z3Ki4eRM2btv6ZaSu5O9x988td8DssVG5mbT0onBe1f79e9YFGipDTMiY85b+fxjRZ9g1LlqjdXhuPVcfzMyT+eTNXE3YGXj1we1s1Y2OKFZe7hcyQ0PIBPRJx64oRW2giJWCxHKKuNX8cVxk7G2104y2lxJNL3DfF1AAGzdlvytx5E/tNIzz3IRnJB7vdz2/Ih/f10GJtVhCUeemL/4iVlygz5jt+Yi/Uj1JlMpBZjTwc1BkFNdF8EQFtKYRngUD63tBqWHPy/h603OWq1EqZ+pDEOhbuBPmZSZak/kv1",
    "If-Modified-Since": "Wed, 12 Jul 2023 06:45:10 GMT"
}


response = requests.get(url, headers=headers)

print(response.headers)

然后写了上面的代码

运行结果

{'Content-Disposition': 'attachment; filename="以å\x9b¾æ\x90\x9cå\x9b¾.pdf"', 'Content-Encoding': 'gzip', 'Content-Type': 'application/pdf', 'Last-Modified': 'Wed, 12 Jul 2023 06:45:10 GMT', 'Server': 'MinIO Console', 'Vary': 'Accept-Encoding', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'DENY', 'X-Xss-Protection': '1; mode=block', 'Date': 'Wed, 12 Jul 2023 07:13:03 GMT', 'Connection': 'close', 'Transfer-Encoding': 'chunked'}

可以看到,用 py 脚本请求,也是乱码

https://segmentfault.com/q/1010000043923867/a-1020000043923969

那么,如何解决呢?

先尝试一下给请求头加上 Accept-Charset: utf-8

但是不行


然后我去 minio 的 github issues 上看了看,发现没有什么有用的信息


我就想,或许这个问题已经在新版的 minio 中解决了


所以我就换了一个新版的 minio

version: "3"
services:
    public_minio:
      container_name: public_minio
      restart: always
      image: minio/minio:RELEASE.2023-07-11T21-29-34Z
      ports:
        - "9000:9000" # client port
        - "9002:9002" # console port
      command: server /data --console-address ":9002" #指定容器中的目录 /data
      volumes:
      - ./volumes/:/data
      environment:
        MINIO_ACCESS_KEY: ponponon #管理后台用户名
        MINIO_SECRET_KEY: ponponon #管理后台密码,最小8个字符
如何查看 minio 的版本

然后问题就解决了

但是这个最新版虽然没有乱码了,但是引入了新的问题:chrome 可以下载文件,但是 firefox 无法下载文件

真是想吐槽一下:minio bug 真多

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏