创建新的云存储桶时获取"HTTP Error 400: Bad Request"



我正在尝试使用GCP API创建一个google存储桶。我收到HTTP错误400:错误的请求错误。不确定我哪里错了。但是curl命令有效。

平台

Python 3.4.3/Uubuntu 14.04.5 LTS

程序:

导入urlib.request导入urlib.parse导入ssl导入http.clientauth=auth=";承载人XXXXXXXXXXXX";

def create_bkt(prj):
quoted_prj = urllib.parse.quote(prj, safe='')
url = 'https://storage.googleapis.com/storage/v1/b?project=%s' % (quoted_prj)
headers = {"Authorization": auth,
"Accept": "application/json",
"Content-Type": "application/json"}
context = ssl._create_unverified_context()
values = {"name":"bkt1"}
data = urllib.parse.urlencode(values).encode("utf-8")
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req, context=context)
if __name__=="__main__":
call1('prj1')
send: b'POST /storage/v1/b?project=file-analyzer HTTP/1.1rnAccept-Encoding: identityrnContent-Type: application/jsonrnConnection: closernAccept: application/jsonrnContent-Length: 12rnUser-Agent: Python-urllib/3.4rnAuthorization: Bearer ya29.A0AfH6SMCbKaWo9g9oo7JAvWtvvuPhfz2EPYU4PI8btAETtsNkS7vpvdeXHkFCb0UX93AzrQIbDKVcxyCdpCNh0DEtYi3cNxM2iZjhTT9QtKlqsVHZoq5xqQJ_mfnCA-VQeTNqpmggWQKe-pPD3HUL1mHV2mpg9ZiLegPji1K4Y30rnHost: storage.googleapis.comrnrnname=uk-bkt9'
reply: 'HTTP/1.1 400 Bad Requestrn'
header: X-GUploader-UploadID header: Content-Type header: Date header: Vary header: Vary header: Cache-Control header: Expires header: Pragma header: Content-Length header: Server header: Alt-Svc header: Connection Traceback (most recent call last):
File "gcs-create-bkt.py", line 52, in <module>
call1('file-analyzer')
File "gcs-create-bkt.py", line 44, in call1
response = urllib.request.urlopen(req, context=context)
File "/usr/lib/python3.4/urllib/request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.4/urllib/request.py", line 469, in open
response = meth(req, response)
File "/usr/lib/python3.4/urllib/request.py", line 579, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.4/urllib/request.py", line 507, in error
return self._call_chain(*args)
File "/usr/lib/python3.4/urllib/request.py", line 441, in _call_chain
result = func(*args)
File "/usr/lib/python3.4/urllib/request.py", line 587, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
curl (works without any issue):
curl -v --request POST 'https://storage.googleapis.com/storage/v1/b?project=prj1' --header 'Authorization: Bearer XXXXXXXXXXXXX' --header 'Accept: application/json'  --header 'Content-Type: application/json' --data '{"name":"bkt1"}' --compressed
{
"kind": "storage#bucket",
"selfLink": "https://www.googleapis.com/storage/v1/b/bkt1",
"id": "bkt1",
"name": "bkt1",
"projectNumber": "676620708324",
"metageneration": "1",
"location": "US",
"storageClass": "STANDARD",
"etag": "CAE=",
"timeCreated": "2020-11-19T18:55:24.287Z",
"updated": "2020-11-19T18:55:24.287Z",
"iamConfiguration": {
"bucketPolicyOnly": {
"enabled": false
},
"uniformBucketLevelAccess": {
"enabled": false
}
},
"locationType": "multi-region"
}

您的值是dict,而不是JSON。

试试这个

import json
def create_bkt(prj):
quoted_prj = urllib.parse.quote(prj, safe='')
url = 'https://storage.googleapis.com/storage/v1/b?project=%s' % (quoted_prj)
headers = {"Authorization": auth,
"Accept": "application/json",
"Content-Type": "application/json"}
context = ssl._create_unverified_context()
values = {"name":"bkt1"}
data = urllib.parse.urlencode(json.dumps(values)).encode("utf-8")
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req, context=context)
...

最新更新