pandas 如何在python中将URL中的图片保存到Azure blob存储ADLS gen2中

tkclm6bt  于 2023-01-07  发布在  Python
关注(0)|答案(1)|浏览(166)

我想直接用python把某个URL中的图片保存到blob存储中。我尝试使用Download web images by URL from excel and save to folders in Python中的代码。这是我如何修改它的

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)

它有错误

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

你知道吗?

ffscu2ro

ffscu2ro1#

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

看看你收到的错误,它可能是因为没有这样的目录,你在路径中提到.
从我这边复制后,我可以用Python Imaging Library在python中实现你的要求,下面是我使用的完整代码。

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))

结果:

相关问题