简体   繁体   English

如何使用 Python 从给定 SAS URI 和容器名称的 Azure Blob 存储下载文件列表?

[英]How to download a list of files from Azure Blob Storage given SAS URI and container name using Python?

I have the container name and its folder structure.我有容器名称及其文件夹结构。 I need to download all files in a single folder in the container using python code .我需要使用python 代码下载容器中单个文件夹中的所有文件。 I also have a SAS URL link to this particular folder.我还有一个指向这个特定文件夹的 SAS URL 链接。

The method I have found online use BlockBlobService class, which is part of the old SDK.我在网上找到的方法使用BlockBlobService类,它是旧 SDK 的一部分。 I need to find a way to do it using the current SDK.我需要找到一种使用当前 SDK 的方法。

Can you please help me with this?你能帮我解决这个问题吗?

Edit 1:编辑1:

this is my SAS URL: https://xxxx.blob.core.windows.net/<CONTAINER>/<FOLDER>?sp=r&st=2022-05-31T17:49:47Z&se=2022-06-05T21:59:59Z&sv=2020-08-04&sr=c&sig=9M8ql9nYOhEYdmAOKUyetWbCU8hoWS72UFczkShdbeY%3D这是我的 SAS 网址: https://xxxx.blob.core.windows.net/<CONTAINER>/<FOLDER>?sp=r&st=2022-05-31T17:49:47Z&se=2022-06-05T21:59:59Z&sv=2020-08-04&sr=c&sig=9M8ql9nYOhEYdmAOKUyetWbCU8hoWS72UFczkShdbeY%3D

Edit 2:编辑2:

added link to the method found.添加了找到的方法的链接。

Edit 3:编辑3:

I also have the full path of the files that I want to download.我也有要下载的文件的完整路径。

Please try this code (untested though).请尝试此代码(尽管未经测试)。

The code below basically parses the SAS URL and creates an instance of ContainerClient .下面的代码主要解析 SAS URL 并创建ContainerClient的实例。 Then it lists the blobs in that container names of which start with the folder name.然后它会列出该容器名称中以文件夹名称开头的 blob。 Once you have that list, you can download individual blobs.获得该列表后,您可以下载单个 blob。

I noticed that your SAS URL only has read permission ( sp=r ).我注意到您的 SAS URL 只有read权限 ( sp=r )。 Please note that you would need both read and list permissions ( sp=rl ).请注意,您需要readlist权限 ( sp=rl )。 You will need to ask for new SAS URL with these two permissions.您将需要请求具有这两个权限的新 SAS URL。

from urllib.parse import urlparse
from azure.storage.blob import ContainerClient

sasUrl = "https://xxxx.blob.core.windows.net/<CONTAINER>/<FOLDER>?sp=r&st=2022-05-31T17:49:47Z&se=2022-06-05T21:59:59Z&sv=2020-08-04&sr=c&sig=9M8ql9nYOhEYdmAOKUyetWbCU8hoWS72UFczkShdbeY%3D"

sasUrlParts = urlparse(sasUrl)

accountEndpoint = sasUrlParts.scheme + '://' + sasUrlParts.netloc

sasToken = sasUrlParts.query

pathParts = sasUrlParts.path.split('/')

containerName = pathParts[1]

folderName = pathParts[2]

containerClient = ContainerClient(accountEndpoint, containerName, sasToken)

blobs = containerClient.list_blobs(folderName)

for blob in blobs_list:
  blobClient = containerClient.get_blob_client(blob)
  download the blob here...blobClient.download()

UPDATE更新

I have the SAS URL mentioned above, and the full paths of the files I want to download.我有上面提到的 SAS URL,以及我要下载的文件的完整路径。 For example: PATH 1 : Container/folder/file1.csv, PATH 2 : Container/folder/file2.txt, and so on例如:PATH 1:Container/folder/file1.csv,PATH 2:Container/folder/file2.txt,等等

Please see the code below:请看下面的代码:

from urllib.parse import urlparse
from azure.storage.blob import BlobClient

sasUrl = "https://xxxx.blob.core.windows.net/<CONTAINER>/<FOLDER>?sp=r&st=2022-05-31T17:49:47Z&se=2022-06-05T21:59:59Z&sv=2020-08-04&sr=c&sig=9M8ql9nYOhEYdmAOKUyetWbCU8hoWS72UFczkShdbeY%3D"

blobNameWithContainer = "Container/folder/file1.csv"

sasUrlParts = urlparse(sasUrl)

accountEndpoint = sasUrlParts.scheme + '://' + sasUrlParts.netloc

sasToken = sasUrlParts.query

blobSasUrl = accountEndpoint + '/' + blobNameWithContainer + '?' + sasToken;

blobClient = BlobClient.from_blob_url(blobSasUrl);
.... now do any operation on that blob ...

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

相关问题 在PYTHON中使用SAS URI从AZURE BLOB CONTAINER下载文件 - Download file from AZURE BLOB CONTAINER using SAS URI in PYTHON 使用 Python 从 azure blob 存储下载文件(csv、excel) - Download files (csv, excel) from azure blob storage using Python 使用 Z23EEEB4347BDD26BDDZ6B7EE9A3B7 中的 SAS URI 将 csv 文件上传到 azure 容器 - Uploading csv files to azure container using SAS URI in python? 如何使用 python 从 blob 容器下载所有文件 - How to download all files from a blob container using python Python - 列出 Azure 存储容器中的所有文件和 blob - Python - List all the files and blob inside an Azure Storage Container Azure Blob存储:使用SAS令牌下载Blob - Azure Blob Storage: Download blob with SAS token 如何使用 Python 和 Azure 函数在 Azure 存储容器中创建 blob - How to create a blob in an Azure Storage Container using Python & Azure Functions 在Python中的Azure Blob存储容器上应用SAS权限 - Apply SAS Permissions on Azure Blob Storage Container in Python 如何使用 Z23EEEB4347BDD26BFC6B7EE9A3B75 生成 azure 存储容器 SAS url - How to generate azure storage container SAS url using python 使用 python azure 函数从 azure blob 存储读取文件 - Read files from azure blob storage using python azure functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM