首页 > 解决方案 > Python: Upload a package to Azure using SAS URI

问题描述

I am using the Microsoft's Hardware dashboard API to automate the submission of my (.CAB) package for signing. I have followed the steps in this documentation: https://docs.microsoft.com/en-us/windows-hardware/drivers/dashboard/create-a-new-submission-for-a-product

The response of new submission contains the SAS(Shared Access Signature) URI like this: (changed the sig and accnt_name for security)

'''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'''

I need to use this SAS URI to upload by package to azure blob storage. The examples in documentation shows C# or .NET as follows:

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);

I want to use the SAS URI obtained from submission resource JSON Response to upload the package.

This link Download file from AZURE BLOB CONTAINER using SAS URI in PYTHON suggests that there is no equivalent method in python and BlockBlobService can be used.

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)

However I am not sure of what is storage_account name and container name from the SAS URI obtained from submission resource.

Also I have created a separate azure storage account and added a new container, blob in it. I have tried passing the new container and storage account name with SAS access token from SAS URI (obtained from submission JSON response micorsoft hardware api) but always get below ERROR

''' AzureHttpError: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. ErrorCode: AuthenticationFailed AuthenticationFailedServer failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:5463b7d2-901e-0068-6994-36782e000000 Time:2019-07-09T20:23:04.5760736ZSignature did not match. String to sign used was rwl

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

2017-04-17

attachment; filename=initial_1152921504628106563.cab ''' Thanks in advance

标签: python-3.xrestazure-blob-storage

解决方案


如果您在下面发布了一个 blob SAS URI,则可以使用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的操作权限,如wBlob Write Permission

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

这是我通过 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的使用宽容器权限的官方示例。


推荐阅读