创建新云存储桶时获取“HTTP错误400:错误请求”

2024-09-27 00:19:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用GCPAPI创建一个google存储桶。我得到HTTP错误400:错误的请求错误。我不知道我会错在哪里。但是curl命令可以工作

平台

Python 3.4.3/Ubuntu 14.04.5 LTS

节目:

导入urllib.request 导入urllib.parse 导入ssl 导入http.client auth=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.1\r\nAccept-Encoding: identity\r\nContent-Type: application/json\r\nConnection: close\r\nAccept: application/json\r\nContent-Length: 12\r\nUser-Agent: Python-urllib/3.4\r\nAuthorization: Bearer ya29.A0AfH6SMCbKaWo9g9oo7JAvWtvvuPhfz2EPYU4PI8btAETtsNkS7vpvdeXHkFCb0UX93AzrQIbDKVcxyCdpCNh0DEtYi3cNxM2iZjhTT9QtKlqsVHZoq5xqQJ_mfnCA-VQeTNqpmggWQKe-pPD3HUL1mHV2mpg9ZiLegPji1K4Y30\r\nHost: storage.googleapis.com\r\n\r\nname=uk-bkt9'
reply: 'HTTP/1.1 400 Bad Request\r\n'
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"
}

Tags: inpyjsonapplicationresponserequestlibusr
1条回答
网友
1楼 · 发布于 2024-09-27 00:19:28

您的值是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)

...

相关问题 更多 >

    热门问题