0

我正在尝试为 excel 文件生成 Blob SAS URL,以读取其数据帧中的数据。我正在使用下面的 python 代码,它在将 URL 值传递给 read_excel 函数时抛出错误“HTTPError:服务器无法验证请求。确保 Authorization 标头的值正确形成,包括签名。”

代码 :

    from azure.storage.blob import generate_blob_sas
    from azure.storage.blob import BlobServiceClient, ResourceTypes, AccountSasPermissions
    from datetime import datetime, timedelta,date
    import pandas as pd
    
    blob_name=<Blobname>
    account_name=<accountname>
    account_key=<accountkey>
    container_name=<blobname>
    
    sas_blob = generate_blob_sas(account_name=account_name, 
                              container_name=container_name,
                                blob_name=blob_name,
                                account_key=account_key,
                                resource_types=ResourceTypes(object=True),
                                permission=AccountSasPermissions(read=True),
                               expiry=datetime.utcnow() + timedelta(hours=1))
    
    blob = generate_blob_sas(account_name,account_key, container_name, blob_name,sas_blob)
    blob_service_client = BlobServiceClient(account_url="https://<account_name>.blob.core.windows.net", credential=sas_blob)
    url = 'https://'+account_name+'.blob.core.windows.net/'+container_name+'/'+blob_name+'?'+sas_blob
    print(url)
    df=pd.read_excel(url, sheet_name='test',usecols=(cols),header=6)

错误 失败 C:\WPy64-3800\python-3.8.0.amd64\lib\site-packages\azure\storage\blob\baseblobservice.py:1009: SyntaxWarning: "is not" with a literal。你的意思是“!=”?如果 lease_duration 不是 -1 并且 \C:\WPy64-3800\python-3.8.0.amd64\lib\site-packages\azure\storage\blob\baseblobservice.py:2660: SyntaxWarning: "is not" with a literal . 你的意思是“!=”?如果lease_duration 不是-1 并且 \C:\WPy64-3800\python-3.8.0.amd64\lib\site-packages\azure\storage\common_connection.py:82: SyntaxWarning: "is" with a literal。你的意思是“==”吗?self.protocol = self.protocol if parsed_url.scheme is '' else parsed_url.schemeTraceback (最近一次调用最后):文件“C:\Temp\rid04ztb.tl0\005b3440-f226-432b-b554-d625411fdb58”,第 26 行,在 df=pd.read_excel(url, sheet_name='test',在里面 在 http_error_default 中引发 HTTPError(req.full_url, code, msg, hdrs, fp)urllib.error.HTTPError: HTTP Error 403: Server failed to authenticate the request。确保 Authorization 标头的值正确形成,包括签名。在 http_error_default 中引发 HTTPError(req.full_url, code, msg, hdrs, fp)urllib.error.HTTPError: HTTP Error 403: Server failed to authenticate the request。确保 Authorization 标头的值正确形成,包括签名。

任何帮助表示赞赏。提前致谢。

4

1 回答 1

0

我相信您收到此错误是因为您将服务 SAS 与帐户 SAS 混合在一起。您的方法不需要resource_types,类型也应该是.generate_blob_saspermissionBlobSasPermissions

请尝试以下代码:

from azure.storage.blob import generate_blob_sas
from azure.storage.blob import BlobServiceClient, ResourceTypes, BlobSasPermissions
from datetime import datetime, timedelta,date
import pandas as pd

blob_name=<Blobname>
account_name=<accountname>
account_key=<accountkey>
container_name=<blobname>

sas_blob = generate_blob_sas(account_name=account_name, 
                            container_name=container_name,
                            blob_name=blob_name,
                            account_key=account_key,
                            permission=BlobSasPermissions(read=True),
                            expiry=datetime.utcnow() + timedelta(hours=1))
于 2021-06-16T05:45:32.583 回答