Python實(shí)現(xiàn)上傳Minio和阿里Oss文件
前言
本文提供Python上傳minio以及阿里oss文件工具,給自己留個(gè)記錄。
環(huán)境依賴
安裝minio以及oss2依賴
pip install minio -i https://pypi.douban.com/simple pip install oss2 -i https://pypi.douban.com/simple
代碼
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/12/10 21:35 # @Author : 劍客阿良_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 S3Error as e: print( "*** minio上傳文件異常 -> {} {}".format(str(e), traceback.format_exc())) raise Exception("minio上傳文件異常:[{}]".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://', '') # 阿里云賬號(hào)AccessKey擁有所有API的訪問權(quán)限,風(fēng)險(xiǎn)很高。強(qiáng)烈建議您創(chuàng)建并使用RAM用戶進(jìn)行API訪問或日常運(yùn)維,請登錄RAM控制臺(tái)創(chuàng)建RAM用戶。 auth = oss2.Auth(access_key, secret_key) # yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。 # 填寫B(tài)ucket名稱。 bucket = oss2.Bucket(auth, _end_point, bucket) # 填寫Object完整路徑和本地文件的完整路徑。Object完整路徑中不能包含Bucket名稱。 # 如果未指定本地路徑,則默認(rèn)從示例程序所屬項(xiàng)目對(duì)應(yīng)本地路徑中上傳文件。 bucket.put_object_from_file(remote_path, local_path) except S3Error as e: print( "*** oss上傳文件異常 -> {} {}".format(str(e), traceback.format_exc())) raise Exception("oss上傳文件異常:[{}]".format(str(e)))
代碼說明:
1、參數(shù)分別為endpoint(IP或者域名:端口)、accessKey、secretKey、桶名、遠(yuǎn)程文件路徑、本地文件路徑。?
補(bǔ)充
Python實(shí)現(xiàn)Minio的下載(主要用于異地備份的中轉(zhuǎn)站)
import logging
from minio import Minio
from minio.error import S3Error
logging.basicConfig(
level=logging.INFO,
filename='../mysqlbackup_downlaod.log',
filemode='a',
format='%(asctime)s %(name)s %(levelname)s--%(message)s'
)
file_name = "mysql_monitor.py"
file_path = "C:\\Users\\lpy\\Desktop\\img\\{}".format(file_name)
def download_file():
# 創(chuàng)建一個(gè)客戶端
minioClient = Minio(
'minio.***.com',
access_key='admin',
secret_key='****',
secure=False
)
try:
minioClient.fget_object(
bucket_name="backup",
object_name="mysql/dev/{}".format(file_name),
file_path=file_path
)
logging.info("file '{0}' is successfully download".format(file_name))
except S3Error as err:
logging.error("download_failed:", err)
if __name__ == '__main__':
download_file()
以上就是Python實(shí)現(xiàn)上傳Minio和阿里Oss文件的詳細(xì)內(nèi)容,更多關(guān)于Python上傳Minio 阿里Oss文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解django的serializer序列化model幾種方法
序列化是將對(duì)象狀態(tài)轉(zhuǎn)換為可保持或傳輸?shù)母袷降倪^程。這篇文章主要介紹了詳解django的serializer序列化model幾種方法。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10使用Python爬蟲庫requests發(fā)送表單數(shù)據(jù)和JSON數(shù)據(jù)
今天再為大家介紹下使用Python爬蟲庫requests發(fā)送表單數(shù)據(jù)和JSON數(shù)據(jù)的方法,這是最基本的使用方法,大家可以參考測試下2020-01-01Python 用turtle實(shí)現(xiàn)用正方形畫圓的例子
今天小編就為大家分享一篇Python 用turtle實(shí)現(xiàn)用正方形畫圓的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11Python編程中的for循環(huán)語句學(xué)習(xí)教程
這篇文章主要介紹了Python編程中的for循環(huán)語句學(xué)習(xí)教程,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10python實(shí)現(xiàn)socket客戶端和服務(wù)端簡單示例
這篇文章主要介紹了python實(shí)現(xiàn)socket客戶端和服務(wù)端簡單示例,需要的朋友可以參考下2014-02-02python實(shí)現(xiàn)二分類和多分類的ROC曲線教程
這篇文章主要介紹了python實(shí)現(xiàn)二分類和多分類的ROC曲線教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06關(guān)于python之字典的嵌套,遞歸調(diào)用方法
今天小編就為大家分享一篇關(guān)于python之字典的嵌套,遞歸調(diào)用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01