赞
踩
- from azure.storage.blob import BlobServiceClient, ContentSettings
-
- connect_str = "Default... 这里是你的连接字符串"
-
-
- class Blob_connection_upload:
- def __init__(self, container_name, img_path, img_name):
- self.connect_str = connect_str
- self.BlobServiceClient = BlobServiceClient
- self.container_name = container_name
- self.img_path = img_path
- self.img_name = img_name
-
- def connection_blob(self):
- try:
- # 创建BlobServiceClient对象
- blob_service_client = self.BlobServiceClient.from_connection_string(self.connect_str)
- containers = blob_service_client.list_containers()
- container_name_list = []
- # 先判断有无该container,若无则创建
- for i in containers:
- container_name_list.append(i["name"])
- if self.container_name not in container_name_list:
- print(self.container_name)
- blob_service_client.create_container(self.container_name)
- # Blob客户端
- blob_client = blob_service_client.get_container_client(container=self.container_name)
- # 上传文件
- with open(self.img_path, "rb") as data:
- blob_client.upload_blob(name=self.img_name, data=data, overwrite=True,
- content_settings=ContentSettings(content_type="image/jpeg"))
- # 获取该图片的blob-url
- blob_url_client = blob_client.get_blob_client(self.img_name)
- blob_url = blob_url_client.url
- return blob_url
- except Exception as e:
- raise e
-
-
- if __name__ == '__main__':
- container_name = 'xr-ceshi'
- img_path = "D:/common_method/files/backup/a.jpg"
- da_kuang_pic_path = "aaa.jpg"
- blob_da_kuang_pic_url = Blob_connection_upload(container_name, img_path, da_kuang_pic_path).connection_blob()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。