您好,登錄后才能下訂單哦!
本篇內容介紹了“Python怎么實現上傳Minio文件”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
安裝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://', '') # 阿里云賬號AccessKey擁有所有API的訪問權限,風險很高。強烈建議您創建并使用RAM用戶進行API訪問或日常運維,請登錄RAM控制臺創建RAM用戶。 auth = oss2.Auth(access_key, secret_key) # yourEndpoint填寫Bucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。 # 填寫Bucket名稱。 bucket = oss2.Bucket(auth, _end_point, bucket) # 填寫Object完整路徑和本地文件的完整路徑。Object完整路徑中不能包含Bucket名稱。 # 如果未指定本地路徑,則默認從示例程序所屬項目對應本地路徑中上傳文件。 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、參數分別為endpoint(IP或者域名:端口)、accessKey、secretKey、桶名、遠程文件路徑、本地文件路徑。
Python實現Minio的下載(主要用于異地備份的中轉站)
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(): # 創建一個客戶端 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怎么實現上傳Minio文件”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。