简体   繁体   English

如何将URL中的图片保存到azure中的blob存储ADLS gen2 in python

[英]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 .我想通过 python 将一些 URL 中的图片直接保存到 blob 存储中。我尝试使用从 excel 下载 web 图片中的代码 URL 并保存到 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 .从我这边复制后,我可以使用Python Imaging Library在 python 中实现您的要求。 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:结果:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Data Lake Storage Gen2 (ADLS Gen2) 作为 Kedro 管道的数据源 - Azure Data Lake Storage Gen2 (ADLS Gen2) as a data source for Kedro pipeline 使用 Python(无 ADB)读取 Azure ADLS Gen2 文件 - Azure ADLS Gen2 File read using Python (without ADB) 如何使用 Python 将文件上传到 ADLS Gen2 - How to upload a file to ADLS Gen2 using Python 使用 Azure CLI、Rest API 或 Python 在 Azure ADLS gen2 中复制文件 - Copy files within Azure ADLS gen2 using Azure CLI, Rest API or Python 使用Python或Java从本地将数据上传到Azure ADLS Gen2 - Upload data to the Azure ADLS Gen2 from on-premise using Python or Java 如何通过读取存储在 Databrciks 中的 adls gen2 中的 csv 文件(特定列)来创建 ADLS gen2 中的文件夹 - how to ceate folders in ADLS gen2 by reading a csv file(particular column) stored in adls gen2 in Databrciks 如何使用 Azure Synapse 和 pySpark 笔记本从 ADLS gen2 检索 .dcm 图像文件? - How to retrieve .dcm image files from the ADLS gen2 using Azure Synapse and pySpark notebook? 如何使用 Python 从 Azure Data Lake Storage Gen2 检索所有目录路径? - How do I retrieve all directory paths from Azure Data Lake Storage Gen2 using Python? 如何从 pyspark 数据块在 ADLS gen2 中创建目录 - How to create directory in ADLS gen2 from pyspark databricks 通过数据块从 ADLS gen2 存储中的多个文件夹中读取文件并创建单个目标文件 - Read files from multiple folders from ADLS gen2 storage via databricks and create single target file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM