gpt4 book ai didi

适用于 Azure blob 的 Python requests.get()

转载 作者:行者123 更新时间:2023-12-02 08:00:44 25 4
gpt4 key购买 nike

我正在尝试从 Azure 存储下载 blob,我更喜欢使用 Python 的 requests.get() 。如果我尝试使用 get_blob_to_path()来自azure ,它可以工作,但不适用于 requests.get() .

我的使用方式如下:

requests.get('https://<account_name>.blob.core.windows.net/<container_name>/<blob_name>')

我还尝试从 Azure 存储资源管理器复制整个 URL。

我从一个帐户收到以下错误:

gaierror: [Errno -2] Name or service not known

以及来自另一个帐户的以下结果(未显示为错误):

<Response [404]>

什么可能导致此错误/<Response [404]>以及如何解决它?通过 requests.get() 连接的权限是否存在一些问题?

最佳答案

该错误是由于您没有指定访问 Blob 存储的权限。

除了@Martin在他的帖子中提到的将公共(public)访问级别更改为容器或blob(截图如下)之外,您还有其他两种方法来解决权限问题。

enter image description here

方法 1:您可以为 blob 生成 SAS URL。导航到 Azure 门户 -> 单击要下载的 Blob 的“...”符号 -> 选择生成 SAS。生成 SAS URL 后,您可以使用 SAS URL 进行 Blob 下载。下面的屏幕截图显示了如何生成 SAS URL:

enter image description here

然后你可以编写如下代码:

#use the SAS URL
r = requests.get('https://yy3.blob.core.windows.net/aa1/w2.JPG?xxxx')
open("d:\\temp\\mytest222.jpg","wb").write(r.content)

方法2:请使用Get Blob rest api ,下面的示例代码对我有用。

import requests
import datetime
import hmac
import hashlib
import base64

storage_account_name = 'xxxx'
storage_account_key = 'xxxxx'
blob_name = 'your_blob_name,like w2.jpg, note it is case sensitive'
container_name='the container name'
api_version = '2018-03-28'
request_time = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')

string_params = {
'verb': 'GET',
'Content-Encoding': '',
'Content-Language': '',
'Content-Length': '',
'Content-MD5': '',
'Content-Type': '',
'Date': '',
'If-Modified-Since': '',
'If-Match': '',
'If-None-Match': '',
'If-Unmodified-Since': '',
'Range': '',
'CanonicalizedHeaders': 'x-ms-date:' + request_time + '\nx-ms-version:' + api_version + '\n',
'CanonicalizedResource': '/' + storage_account_name + '/'+container_name + '/' + blob_name
}

string_to_sign = (string_params['verb'] + '\n'
+ string_params['Content-Encoding'] + '\n'
+ string_params['Content-Language'] + '\n'
+ string_params['Content-Length'] + '\n'
+ string_params['Content-MD5'] + '\n'
+ string_params['Content-Type'] + '\n'
+ string_params['Date'] + '\n'
+ string_params['If-Modified-Since'] + '\n'
+ string_params['If-Match'] + '\n'
+ string_params['If-None-Match'] + '\n'
+ string_params['If-Unmodified-Since'] + '\n'
+ string_params['Range'] + '\n'
+ string_params['CanonicalizedHeaders']
+ string_params['CanonicalizedResource'])

signed_string = base64.b64encode(hmac.new(base64.b64decode(storage_account_key), msg=string_to_sign.encode('utf-8'), digestmod=hashlib.sha256).digest()).decode()

headers = {
'x-ms-date' : request_time,
'x-ms-version' : api_version,
'Authorization' : ('SharedKey ' + storage_account_name + ':' + signed_string)
}

url = ('https://' + storage_account_name + '.blob.core.windows.net/'+container_name+'/'+blob_name)

r = requests.get(url, headers = headers)

#specify where to download and the new file name
open("d:\\temp\\mytest111.jpg","wb").write(r.content)

print("ok")

关于适用于 Azure blob 的 Python requests.get(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57031079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com