Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to upload Minio files with Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to upload Minio files by Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Environmental dependence

Install minio and oss2 dependencies

Pip install minio-I https://pypi.douban.com/simplepip install oss2-I https://pypi.douban.com/simple Code #! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 2021-12-10 21 avatar @ 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 tracebackfrom minio import Miniofrom minio.error import S3Errorimport 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 upload 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://',') '') # Aliyun account AccessKey has access to all API The stakes are high. It is strongly recommended that you create and use RAM users for API access or daily operation and maintenance. Log in to the RAM console to create RAM users. Auth = oss2.Auth (access_key, secret_key) # yourEndpoint enter the Endpoint corresponding to the region where the Bucket resides. Take East China 1 (Hangzhou) as an example, Endpoint is filled in as https://oss-cn-hangzhou.aliyuncs.com. # enter the name of Bucket. Bucket = oss2.Bucket (auth, _ end_point, bucket) # fill in the full path of Object and the full path of the local file. The Bucket name cannot be included in the Object full path. # if no local path is specified, the file is uploaded from the local path corresponding to the project to which the sample program belongs by default. Bucket.put_object_from_file (remote_path, local_path) except S3Error as e: print ("* oss upload exception-> {} {}" .format (str (e), traceback.format_exc ()) raise Exception ("oss upload exception: [{}]" .format (str (e)

Code description:

1. The parameters are endpoint (IP or domain name: Port), accessKey, secretKey, bucket name, remote file path and local file path.

Supplement

Python implements the download of Minio (mainly used as a transit station for remote backup)

Import loggingfrom minio import Miniofrom minio.error import S3Errorlogging.basicConfig (level=logging.INFO, filename='../mysqlbackup_downlaod.log', filemode='a' Format='% (asctime) s% (name) s% (levelname) s Murray% (message) s') file_name = "mysql_monitor.py" file_path = "C:\\ Users\\ lpy\\ Desktop\\ img\ {}" .format (file_name) def download_file (): # create a client 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 () "how to upload Minio files by Python" this ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report