SSL error of urllib3 when Python uploads files using Minio

Problems arise

Minio’s server always uses HTTP for access, but recently, due to business requirements, all requests have been replaced with HTTPS requests. After changing to HTTPS, this error will be reported when uploading the file: urlib3. Exceptions. Maxretryerror: httpsconnectionpool (host = ‘10.10.30.241’, port = 9000): Max retries exceeded with URL:/Facebook?Location = (caused by sslerror (sslcertverificationerror (1, ‘[SSL: Certificate_ VERIFY_ FAILED] certificate verify failed: self signed certificate (_ ssl.c:1124)’)))

Solution 1

Because I saw that the error was reported by urlib, I directly searched the urlib source code and directly modified the urlib 3 source code to solve the problem.
the modified file is: connectionpool.py


modify line 775 of connectionpool.py file:

self.cert_reqs = 'CERT_NONE'

Solution 2

The first method is solved, but it is extremely inconvenient
because you need to modify the source code every time you need to change the deployment script or reinstall it
then I went through the Minio source code and found the problem

the urliib in Minio did not turn off SSL verification. I wanted to change it here, but I thought of changing the source code here. I looked carefully and found the next parameter: http_ client

We only need to customize this parameter when creating Minio objects

    minioClient = Minio(
        '1.1.1.1:9000',
        access_key='mxxxxxxx',
        secret_key='mxxxxxxx',
        http_client=urllib3.PoolManager(
            timeout=urllib3.util.Timeout(connect=10, read=10),
            maxsize=10,
            cert_reqs='CERT_NONE',
            ca_certs= os.environ.get('SSL_CERT_FILE') or certifi.where(),
            retries=urllib3.Retry(
                total=5,
                backoff_factor=0.2,
                status_forcelist=[500, 502, 503, 504]
            )
        )
        # secure=False
    )

So far, the problem is solved

Attach Minio file to upload all codes

import logging
from minio import Minio
from minio.error import S3Error
import urllib3
import certifi
import os
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
logging.basicConfig(
    level=logging.INFO,
    filename='../mysqlbackup_up.log',
    filemode='a',
    format='%(asctime)s %(name)s %(levelname)s--%(message)s'
)



def upload_file(file_name, file_path):

    minioClient = Minio(
        '1.1.3.1:9000',
        access_key='mxxxxx',
        secret_key='mxxxxxx',
        http_client=urllib3.PoolManager(
            timeout=urllib3.util.Timeout(connect=10, read=10),
            maxsize=10,
            cert_reqs='CERT_NONE',
            ca_certs= os.environ.get('SSL_CERT_FILE') or certifi.where(),
            retries=urllib3.Retry(
                total=5,
                backoff_factor=0.2,
                status_forcelist=[500, 502, 503, 504]
            )
        )
        # secure=False
    )

    check_bucket = minioClient.bucket_exists("test")

    if not check_bucket:
        minioClient.make_bucket("test")
    try:
        logging.info("start upload file")
        minioClient.fput_object(bucket_name="test", object_name=file_name, file_path=file_path)
        logging.info("file {0} is successfully uploaded".format(file_name))
        print('success')
    except FileNotFoundError as err:
        print(err)
        logging.error('upload_failed: ' + str(err))
    except S3Error as err:
        print(err)
        logging.error("upload_failed:", err)


Read More: