英文:
Generating SAS URL for Azure Data Lake Storage
问题
我想使用Python从我的计算机访问位于Data Lake存储文件系统文件夹中的文件。我已经生成了SAS令牌并使用以下代码创建了SAS URL。
sas_file = generate_file_sas(account_name=account_name,
file_system_name = file_system_name ,
directory_name = directory_name ,
file_name = file_name,
credential=account_key,
expiry=datetime.utcnow() + timedelta(hours=1)
)
sas_url = 'https://' + account_name+'.blob.core.windows.net/' + container_name + '/' + directory_name + '/' + file_name + '?' + sas_file
但是我无法从这个脚本下载任何文件。我哪里错了?
英文:
I would like to reach my files which located under Data lake storage folder filesystem from my computer using python. I have generated sas token and created sas url with this piece of code
sas_file = generate_file_sas(account_name=account_name,
file_system_name = file_system_name ,
directory_name = directory_name ,
file_name = file_name,
credential=account_key,
expiry=datetime.utcnow() + timedelta(hours=1)
)
sas_url = 'https://' + account_name+'.blob.core.windows.net/' + container_name + '/' + directory_name + '/' + file_name + '?'+ sas_file
but i can't download any file from this script. Where am I wrong?
答案1
得分: 1
我有一个名为 logo.jpg
的文件,位于我的数据湖存储文件系统的目录中,就像这样:
我使用下面的 Python 代码生成 SAS URL 并将文件下载到本地文件夹,如下所示:
from azure.storage.filedatalake import generate_file_system_sas, FileSystemSasPermissions
from datetime import datetime, timedelta
import requests
account_name = "deviadlsgen2acc"
file_system_name = "sri"
directory_name = "dir1"
account_key = "xxxxxxxxxxxxxxxxx"
file_name = "logo.jpg"
file_path = "C:\\Users\\xxxxx\\Documents\\folder1\\test.jpg"
sas_token = generate_file_system_sas(
account_name=account_name,
file_system_name=file_system_name,
credential=account_key,
permission=FileSystemSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1)
)
sas_url = 'https://' + account_name + '.dfs.core.windows.net/' + file_system_name + '/' + directory_name + '/' + file_name + '?' + sas_token
print(sas_url)
response = requests.get(sas_url)
with open(file_path, "wb") as f:
f.write(response.content)
print("文件已成功下载!)
响应:
当我检查该文件路径时,文件已成功下载到本地文件夹,如下所示:
参考:
azure.storage.filedatalake package | Microsoft
英文:
I have one file named logo.jpg
in directory of my Data lake storage folder filesystem like this:
I used below python code to generate SAS URL and download file to local folder like this:
from azure.storage.filedatalake import generate_file_system_sas,FileSystemSasPermissions
from datetime import datetime, timedelta
import requests
account_name="deviadlsgen2acc"
file_system_name="sri"
directory_name="dir1"
account_key="xxxxxxxxxxxxxxxxx"
file_name="logo.jpg"
file_path="C:\\Users\\xxxxx\\Documents\\folder1\\test.jpg"
sas_token = generate_file_system_sas(
account_name=account_name,
file_system_name=file_system_name,
credential=account_key,
permission=FileSystemSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1)
)
sas_url = 'https://' + account_name+'.dfs.core.windows.net/' + file_system_name + '/' + directory_name + '/' + file_name + '?'+ sas_token
print(sas_url)
response = requests.get(sas_url)
with open(file_path, "wb") as f:
f.write(response.content)
print("File downloaded successfully!!!!)
Response:
When I checked that file path, file downloaded successfully to local folder like below:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论