Python实现上传Minio和阿里Oss文件
程序员文章站
2022-03-03 13:45:42
目录前言环境依赖代码补充前言本文提供python上传minio以及阿里oss文件工具,给自己留个记录。环境依赖安装minio以及oss2依赖pip install minio -i https://p...
前言
本文提供python上传minio以及阿里oss文件工具,给自己留个记录。
环境依赖
安装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和阿里oss文件的详细内容,更多关于python上传minio 阿里oss文件的资料请关注其它相关文章!
上一篇: EMC实施Saas战略