iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python怎么实现上传Minio文件
  • 923
分享到

Python怎么实现上传Minio文件

2023-06-25 22:06:28 923人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

本篇内容介绍了“python怎么实现上传MiNIO文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!环境依赖安装minio以及oss2依赖p

本篇内容介绍了“python怎么实现上传MiNIO文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

环境依赖

安装minio以及oss2依赖

pip install minio -i https://pypi.douban.com/simplepip 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 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上传文件异常 -> {} {}".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)))

代码说明:

参数分别为endpoint(IP或者域名:端口)、accessKey、secreTKEy、桶名、远程文件路径、本地文件路径。 

补充

Python实现Minio的下载(主要用于异地备份的中转站)

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--%(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文件”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

--结束END--

本文标题: Python怎么实现上传Minio文件

本文链接: https://www.lsjlt.com/news/306334.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作