简体   繁体   中英

how to save the picture in a URL into azure blob storage ADLS gen2 in python

I would like to save the picture in some URL into a blob storage directly by python. I tried to use the code in Download web images by URL from excel and save to folders in Python . This is how I adapted it

for index, row in data.iterrows():
    url = row['url']
    file_name = url.split('/')[-1]
    r = requests.get(url)
    abs_file_name = lake_reporting_root + file_name #blob storage folder name
    if r.status_code == 200:
        with open(abs_file_name, "wb") as f:
            f.write(r.content)

It has an error

FileNotFoundError: [Errno 2] No such file or directory:'abfss://datalake@xxx.dfs.core.windows.net/production/xx/test/xxxx

Any idea?

FileNotFoundError: [Errno 2] No such file or directory:

Looking at the error you are receiving, It might be because there is no such directory as you mentioned in the path.

After reproducing from my end, I could able to achieve your requirement in python using Python Imaging Library . Following is the complete code that worked for me.

from azure.storage.filedatalake import DataLakeServiceClient
from PIL import Image
import requests
from io import BytesIO

ACCOUNT_NAME = "<Account_Name>"
CONTAINER_NAME = "<Container_Name>"
ACCESS_KEY='<Access_Key>'

service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
            "https", ACCOUNT_NAME), credential=ACCESS_KEY)

file_system_client = service_client.get_file_system_client(file_system=CONTAINER_NAME) # gets container client
directory_client = file_system_client.get_directory_client("abc/xyz") # gets directory client

url = '<Image_URL>'
file_name = url.split('/')[-1]
response = requests.get(url)
img = Image.open(BytesIO(response.content))

file_client = directory_client.create_file(file_name) #c reates a file in the respective path/ directory
file_client.append_data(data=response.content, offset=0, length=len(response.content))
file_client.flush_data(len(response.content))

RESULTS:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM