rabbitmq + minio +python 上传文件
功能实现
- RabbitMq接收hello里面传来的消息
- 根据消息在 MobileFile里面新建文件
- 新建文件上传到minio
python 新建文件
import os
path = './MobileFile'
file_path = os.path.join(path,"new_file.txt")
with open(file_path, "w") as file:
pass
转换成函数格式
import os
def createfile(filename):
path = '.\\MobileFile'
file_path = os.path.join(path,f"{filename}")
print(file_path)
with open(file_path, "w") as file:
pass
minio上传文件函数
def upfile(body):
# 上传的文件
source_file = f"./MobileFile/{body}"
# The destination bucket and filename on the MinIO server
bucket_name = "python-test-bucket"
destination_file = f"{body}"
# 如果桶不存在就新建
found = client.bucket_exists(bucket_name)
if not found:
client.make_bucket(bucket_name)
# 上传文件
client.fput_object(
bucket_name, destination_file, source_file,
)
print(
source_file, "successfully uploaded as object",
destination_file, "to bucket", bucket_name,
)
rabbitMq消息问题
rabbitMq传送过来的消息是字节,我们要进行一个字节的转换
字节转换函数
def typechangetoNomal(body):
strbody = str(body)
return body[2:len(body)-1]
原理,打印出来的是 b’body’
我们只需要打印出body即可,也就是说,转换成字符串之后选取第三个到最后第二个就行。
callBack函数(即收到消息执行的函数)
def callback(ch, method, properties, body):
body = typechangetoNomal(body)
# print(newbody)
#新建文件夹 已完成
myFileController.createfile(body)
#上传到minio里面
myMinio.upfile(body)
问题发现
python文件名如果带数字就import出现问题