如何访问 Python google.cloud.storage 上传方法中的错误原因?
How to access error reason in Python google.cloud.storage upload methods?
我正在使用 Google 的 google-cloud-storage
Python 包进行 GCS 访问。当我收到 403 错误时,可能有很多不同的原因。 Google的SDK默认只提供这条消息:
('Request failed with status code', 403, 'Expected one of', <HTTPStatus.OK: 200>)")
使用调试器,我可以更深入地查看库并发现 _upload.py
有一个 _process_response
方法,可以在其中找到真正的 HTTP 响应,结果中包含以下消息:
"message": "$ACCOUNT does not have storage.objects.delete access to $BLOB."
问:有什么方法可以访问这个更有用的错误代码或原始响应吗?
我希望向用户展示两者之间的区别。凭据过期并试图执行您的凭据不允许的操作。
您使用的 google-cloud-storage
是什么版本?用最新的,和这个例子:
from google.cloud import storage
client = storage.Client.from_service_account_json('service-account.json')
bucket = client.get_bucket('my-bucket-name')
blob = bucket.get_blob('test.txt')
try:
blob.delete()
except Exception as e:
print(e)
它打印以下内容:
403 DELETE https://storage.googleapis.com/storage/v1/b/my-bucket-name/o/test.txt?generation=1579627133414449: $ACCOUNT does not have storage.objects.delete access to my-bucket-name/test.txt.
这里的字符串表示和e.message
:
大致相同
>>> e.message
'DELETE https://storage.googleapis.com/storage/v1/b/my-bucket-name/o/test.txt?generation=1579627133414449: $ACCOUNT does not have storage.objects.delete access to my-bucket-name/test.txt.'
如果你想要更多的结构,你可以使用e._response.json()
:
>>> e._response.json()
{
'error': {
'code': 403,
'message': '$ACCOUNT does not have storage.objects.delete access to my-bucket-name/test.txt/test.txt.',
'errors': [{
'message': '$ACCOUNT does not have storage.objects.delete access to my-bucket-name/test.txt/test.txt.',
'domain': 'global',
'reason': 'forbidden'
}]
}
}
我正在使用 Google 的 google-cloud-storage
Python 包进行 GCS 访问。当我收到 403 错误时,可能有很多不同的原因。 Google的SDK默认只提供这条消息:
('Request failed with status code', 403, 'Expected one of', <HTTPStatus.OK: 200>)")
使用调试器,我可以更深入地查看库并发现 _upload.py
有一个 _process_response
方法,可以在其中找到真正的 HTTP 响应,结果中包含以下消息:
"message": "$ACCOUNT does not have storage.objects.delete access to $BLOB."
问:有什么方法可以访问这个更有用的错误代码或原始响应吗?
我希望向用户展示两者之间的区别。凭据过期并试图执行您的凭据不允许的操作。
您使用的 google-cloud-storage
是什么版本?用最新的,和这个例子:
from google.cloud import storage
client = storage.Client.from_service_account_json('service-account.json')
bucket = client.get_bucket('my-bucket-name')
blob = bucket.get_blob('test.txt')
try:
blob.delete()
except Exception as e:
print(e)
它打印以下内容:
403 DELETE https://storage.googleapis.com/storage/v1/b/my-bucket-name/o/test.txt?generation=1579627133414449: $ACCOUNT does not have storage.objects.delete access to my-bucket-name/test.txt.
这里的字符串表示和e.message
:
>>> e.message
'DELETE https://storage.googleapis.com/storage/v1/b/my-bucket-name/o/test.txt?generation=1579627133414449: $ACCOUNT does not have storage.objects.delete access to my-bucket-name/test.txt.'
如果你想要更多的结构,你可以使用e._response.json()
:
>>> e._response.json()
{
'error': {
'code': 403,
'message': '$ACCOUNT does not have storage.objects.delete access to my-bucket-name/test.txt/test.txt.',
'errors': [{
'message': '$ACCOUNT does not have storage.objects.delete access to my-bucket-name/test.txt/test.txt.',
'domain': 'global',
'reason': 'forbidden'
}]
}
}