Azure-blob-storage_4.py 888 B

123456789101112131415161718192021
  1. def create_blob_from_url(storage_connection_string,container_name):
  2. try:
  3. # urls to fetch into blob storage
  4. url_list = get_random_images()
  5. # Instantiate a new BlobServiceClient and a new ContainerClient
  6. blob_service_client = BlobServiceClient.from_connection_string(storage_connection_string)
  7. container_client = blob_service_client.get_container_client(container_name)
  8. for u in url_list:
  9. # Download file from url then upload blob file
  10. r = requests.get(u, stream = True)
  11. if r.status_code == 200:
  12. r.raw.decode_content = True
  13. blob_client = container_client.get_blob_client(get_filename_from_url(u))
  14. blob_client.upload_blob(r.raw,overwrite=True)
  15. return True
  16. except Exception as e:
  17. print(e.message, e.args)
  18. return False