current position:Home>Python file upload Minio and Ali OSS tools | Python tools

Python file upload Minio and Ali OSS tools | Python tools

2022-02-02 09:46:49 Swordsman a Liang_ ALiang

Catalog

Preface

Environment depends on

Code

summary


Preface

This article provides Python Upload minio And Ali oss File tools , Keep a record for yourself .

Environment depends on

install minio as well as oss2 rely on

pip install minio -i https://pypi.douban.com/simple
pip install oss2 -i https://pypi.douban.com/simple

Code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/12/10 21:35
# @Author  :  Swordsman a Liang _ALiang
# @Site    : 
# @File    : upload_tool.py
# !/user/bin/env python
# coding=utf-8
"""
@project : dh_train
@author  : huyi
@file   : remote_upload_util.py
@ide    : PyCharm
@time   : 2021-12-10 14:58:29
"""
import traceback
from minio import Minio
from minio.error import S3Error
import oss2


def minio_file_upload(end_point: str, access_key: str, secret_key: str, bucket: str, remote_path: str, local_path: str):
    try:
        _end_point = end_point.replace('https://', '').replace('http://', '')
        # Create a client with the MinIO server playground, its access key
        # and secret key.
        client = Minio(
            _end_point,
            access_key=access_key,
            secret_key=secret_key,
            secure=False
        )

        # Make 'asiatrip' bucket if not exist.
        found = client.bucket_exists(bucket)
        if not found:
            client.make_bucket(bucket)
        else:
            print("Bucket {} already exists".format(bucket))

        # Upload '/home/user/Photos/asiaphotos.zip' as object name
        # 'asiaphotos-2015.zip' to bucket 'asiatrip'.
        client.fput_object(
            bucket, remote_path, local_path,
        )
        print(
            "{} is successfully uploaded as "
            "object {} to bucket {}.".format(local_path, remote_path, bucket)
        )
    except Exception as e:
        print(
            "*** minio Upload file exception  -> {} {}".format(str(e), traceback.format_exc()))
        raise Exception("minio Upload file exception :[{}]".format(str(e)))


def oss_file_upload(end_point: str, access_key: str, secret_key: str, bucket: str, remote_path: str, local_path: str):
    try:
        _end_point = end_point.replace('https://', '').replace('http://', '')
        #  Alicloud account AccessKey Have all the API Access rights of , The risk is high . It is highly recommended that you create and use it RAM The user carries out API Visit or daily operations , Please log in RAM Console creation RAM user .
        auth = oss2.Auth(access_key, secret_key)
        # yourEndpoint Fill in Bucket The region corresponding to Endpoint. East China 1( Hangzhou ) For example ,Endpoint Fill in for https://oss-cn-hangzhou.aliyuncs.com.
        #  Fill in Bucket name .
        bucket = oss2.Bucket(auth, _end_point, bucket)

        #  Fill in Object Full path and full path of local file .Object The full path cannot contain Bucket name .
        #  If no local path is specified , By default, the file is uploaded from the local path corresponding to the project to which the sample program belongs .
        bucket.put_object_from_file(remote_path, local_path)
    except Exception as e:
        print(
            "*** oss Upload file exception  -> {} {}".format(str(e), traceback.format_exc()))
        raise Exception("oss Upload file exception :[{}]".format(str(e)))

Code instructions :

1、 The parameters are endpoint(IP Or domain name : port )、accessKey、secretKey、 Barrel name 、 Remote file path 、 Local file path .

summary

There's nothing to summarize , Keep a record for yourself .

Share :

        Civilize the years , Instead of giving civilization time .——《 Trisomy 》

If this article is useful to you , Give me a compliment , thank you !

copyright notice
author[Swordsman a Liang_ ALiang],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202020946477192.html

Random recommended