当前位置: 首页 > article >正文

python本地连接minio

在你浏览器能成功访问到你的minio网页,并且成功登录之后。接下来如果你想用python连接数据库,并且想用python连接minio,就可以用这个blog。

连接代码

client = Minio("localhost:9000",  #9000是默认端口号
    access_key="admin",  #你的账户
    secret_key="password",  #你的密码
    secure= False,  #这点我会详细说明
)

为什么要用到这行代码

    secure= False,  #这点我会详细说明

如果不用的话,你可能会报这样的错误

Traceback (most recent call last): File "D:\environment\python\Lib\site-packages\urllib3\connection.py", line 198, in _new_conn sock = connection.create_connection( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\environment\python\Lib\site-packages\urllib3\util\connection.py", line 85, in create_connection raise err File "D:\environment\python\Lib\site-packages\urllib3\util\connection.py", line 73, in create_connection sock.connect(sa) ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

这里涉及到了ulib库里面底层的东西,这里直接给大家看minio库里面的逆天代码

self._base_url = BaseURL(  
    ("https://" if secure else "http://") + endpoint,  
    region,  
)

这是minio官方包里面的代码,就是如果你secure(默认为true)true,他就会把你的"localhost:9000"这行代码前面+上https//,但是由于我们本地是http而不是https然后就会出现这样的bug,而不是什么端口开放的问题。

查看桶是否存在 创建桶

bucket_name = "python-test-bucket"
#查看桶是否存在
found = client.bucket_exists(bucket_name)
if not found:  
	#创建桶
    client.make_bucket(bucket_name)  
    print("Created bucket", bucket_name)

这段代码的意思是查看桶是否存在,如果不存在就创建桶。

上传文件

#桶名字
bucket_name = "python-test-bucket"
#上传后文件的名字
destination_file = "my-test-file.txt"
#要上传的文件
source_file = "./tmp/test-file.txt"

client.fput_object(  
    bucket_name, destination_file, source_file,  
)

这段代码的意思是,在上传 你本机./tmp/test-file.txt文件,并命名成my-test-file.txt(destination_file),到python-test-bucket桶里面。

合起来代码就是

from minio import Minio
client = Minio("localhost:9000",  #9000是默认端口号
    access_key="admin",  #你的账户
    secret_key="password",  #你的密码
    secure= False,  #这点我会详细说明
)

bucket_name = "python-test-bucket"
#查看桶是否存在
found = client.bucket_exists(bucket_name)
if not found:  
	#创建桶
    client.make_bucket(bucket_name)  
    print("Created bucket", bucket_name)

#上传后文件的名字
destination_file = "my-test-file.txt"
#要上传的文件
source_file = "./tmp/test-file.txt"

client.fput_object(  
    bucket_name, destination_file, source_file,  
)

http://www.kler.cn/a/592449.html

相关文章:

  • 【Android】安卓 Java下载ZIP文件并解压(笔记)
  • 知识图谱知识库汇总
  • 算法刷题区域部分反转
  • Git 新建本地分支并关联到远程仓库
  • RG-S3760应用协议配置
  • 基于javaweb的SpringBoot药房管理系统设计与实现(源码+文档+部署讲解)
  • 逆向入门 汇编语言IDA的基本使用
  • 【java面型对象进阶】------继承实例
  • VBA技术资料MF281:驱动器列表和类型
  • 学习threejs,构建THREE.ParametricGeometry参数化函数生成几何体
  • 基于 Quest 摄像头数据开发的原理介绍【Unity Meta Quest MR 开发教程】
  • 微前端 qiankun vite vue3
  • 使用码云搭建CocoaPods远程私有库
  • 人事档案管理系统基于Spring BootSSM
  • LS-NET-006-思科MDS 9148S 查看内存
  • 【微服务】基于Lambda ESM的预留模式调整Kafka ESM吞吐量的实战
  • Spring Boot集成MyBatis与MySQL
  • Swagger-告别手写文档
  • 第十五届蓝桥杯C/C++组:宝石组合题目(从小学奥数到编程题详解)
  • 【嵌入式Linux】基于ArmLinux的智能垃圾分类系统项目