在Python中通过SAS导入azure blob

时间:2017-10-24 12:05:37

标签: python azure azure-storage azure-storage-blobs

编辑: 我希望通过Azure Storage ContainerBLOB-specific SAS中的blob导入到我的Python脚本中。

from azure.storage.blob import BlobService

sas_service = BlobService(
    account_name = "name",
    sas_token = "mytoken"
)

blob_content = sas_service.get_blob_to_path("container_name", "blob_name")

我尝试使用此功能,但它会输出一个OSError列表,而且会出现" 503错误"

2 个答案:

答案 0 :(得分:2)

根据您的说明,您希望access azure blob storage通过SAS_TOKEN

您可以参考下面的代码片段,它对我有用:

from datetime import datetime, timedelta
import requests
from azure.storage.blob import (
    BlockBlobService,
    ContainerPermissions,
)

accountName = "<your_account_name>"
accountKey = "<your_account_key>"
containerName = "<your_container_name>"
blobName = "<your_blob_name>"

def GetSasToken():
    blobService = BlockBlobService(account_name=accountName, account_key=accountKey)
    sas_token = blobService.generate_container_shared_access_signature(containerName,ContainerPermissions.READ, datetime.utcnow() + timedelta(hours=1))
    return sas_token


def AccessTest(token):
    blobService = BlockBlobService(account_name = accountName, account_key = None, sas_token = token)
    blobService.get_blob_to_path(containerName,blobName,"E://test.txt")


token=GetSasToken()
print token
AccessTest(token)

您还可以参考official tutorial中的更多详情。

希望它对你有所帮助。

答案 1 :(得分:-2)