[英]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.