Python:使用 SAS URI 将包上传到 Azure



我正在使用Microsoft的硬件仪表板API来自动提交我的(.CAB)包进行签名。我已按照本文档中的步骤操作:https://learn.microsoft.com/en-us/windows-hardware/drivers/dashboard/create-a-new-submission-for-a-product

新提交的响应包含 SAS(共享访问签名)URI 像这样:(为了安全起见更改了 SIG 和accnt_name)

'''https://accnt_name.blob.core.windows.net/scsjc/cexxx 3Dinitial_xxxxxxxx.cab x

我需要使用此 SAS URI 按包上传到 Azure Blob 存储。文档中的示例显示了 C# 或 .NET,如下所示:

string sasUrl = 
"https://productingestionbin1.blob.core.windows.net/ingestion/26920f66- 
b592-4439-9a9d-fb0f014902ec?sv=2014-02-
14&sr=b&sig=usAN0kNFNnYE2tGQBI%2BARQWejX1Guiz7hdFtRhyK%2Bog%3D&se=2016- 
06-17T20:45:51Z&sp=rwl";
Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBob =
new Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob(new 
System.Uri(sasUrl));
await blockBob.UploadFromStreamAsync(stream);

我想使用从提交资源 JSON 响应获取的 SAS URI 上传包。

此链接使用 PYTHON 中的 SAS URI 从 AZURE BLOB CONTAINER 下载文件表明,python 中没有等效的方法,可以使用 BlockBlobService。

from azure.storage.blob import BlockBlobService
blobservice = BlockBlobService("storage_account",sas_token="?sv=2018-03- 
28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-04-24T10:01:58Z&st=2019-04- 
23T02:01:58Z&spr=https&sig=xxxxxxxxx")
blobservice.create_blob_from_path(container_name, local_file_name, 
full_path_to_file)

但是,我不确定从提交资源获得的 SAS URI 中的storage_account名称和容器名称是什么。

此外,我还创建了一个单独的 Azure 存储帐户,并在其中添加了一个新的容器 blob。我尝试使用SAS URI中的SAS访问令牌传递新的容器和存储帐户名称(从提交JSON响应micorsoft硬件api获得),但总是低于错误

''' AzureHttpError: 服务器无法对请求进行身份验证。确保授权标头的值格式正确,包括签名。错误代码:身份验证失败身份验证失败服务器无法对请求进行身份验证。确保授权标头的值格式正确,包括签名。 请求 ID:5463b7d2-901e-0068-6994-36782e000000 时间:2019-07-09T20:23:04.5760736ZSignature不匹配。使用的要签名的字符串是 rwl

2019-07-10T18:15:58Z/blob/evcertautomation/ev2/initial_1152921504628106590.cab

2017年04月17

日附件;文件名=initial_1152921504628106563.cab ''' 提前致谢

如果你有一个blob SAS URI,如你在下面发布的那样,你可以轻松地将文件上传到Python中的blob,requests

https://accnt_name.blob.core.windows.net/scsjc/cexxxxxxxxxx?sv=2017-04-17&sr=b&sig=xxxxxxxxxxxxxx&se=2019-07-10T18:15:58Z&sp=rwl&rscd=attachment%3B filename%3Dinitial_xxxxxxxx.cab

首先,您必须检查参数的值sesp.se参数表示 blob SAS URI 的过期时间,sp参数表示 blob SAS URL 的操作权限,如 Blob 写入权限的w

因此,对于上面的 blob SAS URL,你具有 blob 写入权限,可以在时间2019-07-10T18:15:58Z之前将文件上传到此 Blob。

下面是通过 blob sas uri 上传的示例代码。

import requests
blob_sas_uri = '<your blob sas uri which must includes `sp=w` and do the write operation before `se`>'
local_file_name = '<your local file name>'
headers = {
'x-ms-blob-type': 'BlockBlob'
}
data = open(local_file_name).read()
r = requests.put(blob_sas_uri, headers=headers, data=data)
print(r.status_code)

如果您看到结果为201,它工作正常并成功上传。

作为参考,有一个类似的官方示例Example: Upload a Blob using a Container’s Shared Access Signature它使用广泛的容器权限。

根据您提供的 SAS URI: '''https://accnt_name.blob.core.windows.net/scsjc/cexxxxx?sv=2017-04-17&sr=b&sig=xxxx 3Dinitial_xxxxxxxx.cab x

帐户名称应accnt_name,容器应为scsjc

所以你的代码应该如下所示:

from azure.storage.blob import BlockBlobService
storage_account ="accnt_name"
token="?sv=2018-03- 
28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-04-24T10:01:58Z&st=2019-04- 
23T02:01:58Z&spr=https&sig=xxxxxxxxx"
container="scsjc"
blobservice = BlockBlobService(storage_account,sas_token=token)
blobservice.create_blob_from_path(container, local_file_name, 
full_path_to_file)

最新更新