Python:使用SAS URI将包上载到Azure [英] Python: Upload a package to Azure using SAS URI
问题描述
我正在使用微软的硬件仪表板API自动执行我的(.CAB)包的深渊翻滚签名。我已按照本文档中的步骤操作:https://docs.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/cexxxxxxxxxx?sv=2017-04-17&sr=b&sig=xxxxxxxxxxxxxx&se=2019-07-10T18:15:58Z&sp=rwl&rscd=attachment%3B文件名%3Dinitial_xxxxxxx.cab‘
我需要使用此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 RESPONSE中获取的SASURI上传包。
此链接Download file from AZURE BLOB CONTAINER using SAS URI in PYTHON表示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)
但是,我不确定从深渊翻滚资源获取的SASURI中的STORAGE_ACCOUNT名称和CONTAINER名称是什么。
我还创建了一个单独的azure存储帐户,并在其中添加了一个新容器blob。我已尝试从SASURI(从深渊翻滚JSON Response Microsoft硬件API获得)传递带有SAS访问令牌的新容器和存储帐户名,但始终出现以下错误""
AzureHttpError:服务器无法对请求进行身份验证。确保包括签名在内的Authorization Header的值格式正确。错误代码:身份验证失败
AuthenticationFailed
服务器无法对请求进行身份验证。确保包括签名在内的Authorization Header的值格式正确。
RequestId:5463b7d2-901e-0068-6994-36782e000000
时间:2019-07-09T20:23:04.5760736Z签名不匹配。要签名的字符串使用的是RWL
2019-07-10T18:15:58Z /blob/evcertautomation/ev2/initial_1152921504628106590.cab
2017-04-17
附件;文件名=Initial_1152921504628106563.cab """ 提前感谢推荐答案
如果您有下面发布的blob SAS URI,您可以使用requests
轻松地将文件上载到Python中的blob。
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
se
和sp
的值。se
参数表示BLOB SAS URI的过期时间,sp
参数表示BLOB SAS URL的操作权限,如w
FOR BLOB写入权限
因此,对于上面的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
,使用的容器权限很大。
这篇关于Python:使用SAS URI将包上载到Azure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!