当前位置:   article > 正文

Azure blob:python实现上传图片功能_azure python 实现上传图片

azure python 实现上传图片
  1. from azure.storage.blob import BlobServiceClient, ContentSettings
  2. connect_str = "Default... 这里是你的连接字符串"
  3. class Blob_connection_upload:
  4. def __init__(self, container_name, img_path, img_name):
  5. self.connect_str = connect_str
  6. self.BlobServiceClient = BlobServiceClient
  7. self.container_name = container_name
  8. self.img_path = img_path
  9. self.img_name = img_name
  10. def connection_blob(self):
  11. try:
  12. # 创建BlobServiceClient对象
  13. blob_service_client = self.BlobServiceClient.from_connection_string(self.connect_str)
  14. containers = blob_service_client.list_containers()
  15. container_name_list = []
  16. # 先判断有无该container,若无则创建
  17. for i in containers:
  18. container_name_list.append(i["name"])
  19. if self.container_name not in container_name_list:
  20. print(self.container_name)
  21. blob_service_client.create_container(self.container_name)
  22. # Blob客户端
  23. blob_client = blob_service_client.get_container_client(container=self.container_name)
  24. # 上传文件
  25. with open(self.img_path, "rb") as data:
  26. blob_client.upload_blob(name=self.img_name, data=data, overwrite=True,
  27. content_settings=ContentSettings(content_type="image/jpeg"))
  28. # 获取该图片的blob-url
  29. blob_url_client = blob_client.get_blob_client(self.img_name)
  30. blob_url = blob_url_client.url
  31. return blob_url
  32. except Exception as e:
  33. raise e
  34. if __name__ == '__main__':
  35. container_name = 'xr-ceshi'
  36. img_path = "D:/common_method/files/backup/a.jpg"
  37. da_kuang_pic_path = "aaa.jpg"
  38. blob_da_kuang_pic_url = Blob_connection_upload(container_name, img_path, da_kuang_pic_path).connection_blob()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/541089
推荐阅读
相关标签
  

闽ICP备14008679号