【问题标题】:sas generated blob url sends bad request 400 to azure ocr apisas 生成的 blob url 向 azure ocr api 发送错误请求 400
【发布时间】:2019-08-29 18:08:45
【问题描述】:

我已将一些图像上传到 azure blob 容器,以便我的 azure ocr api 读取图像并发送回输出。

我已经使用 azure 容器检索了 blob 列表

 blob_service.list_blobs().

每个检索到的 blob 现在都提供给共享访问方法

generate_blob_shared_access_signature(container_name='ocr-images',blob_name=blob.name,permission=PublicAccess.OFF,expiry='se=2015-04-30T02%3A23%3A26Z',start='st=2015-04-29T22%3A18%3A26Z')

共享访问方法的输出是一个 SAS 令牌,然后将其提供给

blob_service.make_blob_url(container_name='ocr-images',blob_name=blob.name, sas_token=sas)

为每张图片生成url

传递给 Azure ocr api 时生成的 URL,显示错误

requests.exceptions.HTTPError: 400 Client Error: Bad Request for url:https://westeurope.api.cognitive.microsoft.com/vision/v2.0/ocr?language=unk&detectOrientation=false

但是当从 azure 门户手动生成 url 时,它可以完美运行。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: python azure azure-blob-storage azure-sdk-python


    【解决方案1】:

    您收到此错误是因为您错误地使用了 generate_blob_shared_access_signature 方法。

    您传递给此方法的值存在许多问题。

    对于permission,您需要提供BlobPermissions 的可能值之一。假设您想阅读 blob 内容,我建议您使用 BlobPermissions.READ 权限。

    您的开始日期和到期日期都在过去。此外,您只需要指定日期值,不包括st=se=

    请尝试以下代码:

    generate_blob_shared_access_signature(container_name='ocr-images',blob_name=blob.name,permission=BlobPermissions.READ,expiry='2019-04-09',start='2019-04-08')
    

    【讨论】:

    • 感谢您的回复。我使用了上面提到的代码,现在我无法访问 blob。当我在浏览器中打开生成的 url 时,它显示 404 错误
    • 请分享您正在使用的 SAS URL。
    • sas 令牌:st=2019-04-09&se=2019-04-10&sp=r&sv=2018-03-28&sr=b&sig=TgkeUIHuv3uiFppMMpByJQahwwPugem6J8SZ/Z2Iv7M%3D 和 sas url:externalimages.blob.core.windows.net/ocr-images/…987654322@
    • 请确保 blob 容器 ocr-images 存在于您的存储帐户中。当我尝试访问该 URL 时,我收到了 The specified container does not exist 的消息。
    • 我找到了答案。我必须创建一个 BlobSharedAccessSignature() 对象并为 sas 令牌使用 generate_blob() 方法。未使用权限创建 sas 令牌,因此未加载图像。任何它是如何完成的。谢谢你帮助我
    【解决方案2】:

    如果在从 SAS 令牌 blob 创建 url 时出现 404 错误,则需要从 BlobSharedAccesssignature 创建一个 SAS 令牌。

    这是我的代码:

    from azure.storage.blob import BlockBlobService
    from azure.storage.blob.models import BlobPermissions
    from azure.storage.blob.sharedaccesssignature import BlobSharedAccessSignature
    
    account_name = data_dict['blob_storage_account_name']
    account_key = data_dict['blob_storage_account_key']
    top_level_container_name = data_dict['blob_container_name']
    blob_service = BlockBlobService(account_name, account_key)
    blob_shared = BlobSharedAccessSignature(account_name, account_key)
    

    从 BlobSharedAccessSignature 获得对象后,调用 generate_blob 方法,该方法为容器中的单个 blob 创建 sas 令牌

    sas = blob_shared.generate_blob(container_name=top_level_container_name, blob_name=blob.name,
                                         permission=BlobPermissions.READ, expiry='2019-04-10',
                                         start='2019-04-09')   
    
    sas_url= 'https://'+account_name+'.blob.core.windows.net'+'/'+top_level_container_name+'/'+blob.name+'?'+sas
    

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 2020-10-12
      • 2013-11-05
      • 2020-05-14
      相关资源
      最近更新 更多