本文介绍了在Python中通过SAS导入天蓝色blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 BLOB专用SAS Azure存储容器中的blob导入到我的Python脚本中.

I am looking to import a blob from an Azure Storage Container into my Python script via a BLOB-specific SAS.

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错误"

I tried using this, but it outputs an OSError listing also a "503 error"

推荐答案

根据您的描述,您想通过 SAS_TOKEN 访问天蓝色blob存储.

According to your description , you want to access azure blob storage via SAS_TOKEN.

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

You could refer to the snippet of code as below which works for me:

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)

您还可以从官方教程.

希望它对您有帮助.

这篇关于在Python中通过SAS导入天蓝色blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:39