生成Azure Data Lake Storage的SAS URL

huangapple go评论66阅读模式
英文:

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 的文件,位于我的数据湖存储文件系统的目录中,就像这样:

生成Azure Data Lake Storage的SAS URL

我使用下面的 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 Data Lake Storage的SAS URL

当我检查该文件路径时,文件已成功下载到本地文件夹,如下所示:

生成Azure Data Lake Storage的SAS URL

参考:
azure.storage.filedatalake package | Microsoft

英文:

I have one file named logo.jpg in directory of my Data lake storage folder filesystem like this:

生成Azure Data Lake Storage的SAS URL

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:

生成Azure Data Lake Storage的SAS URL

When I checked that file path, file downloaded successfully to local folder like below:

生成Azure Data Lake Storage的SAS URL

Reference:
azure.storage.filedatalake package | Microsoft

huangapple
  • 本文由 发表于 2023年6月8日 17:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定