【问题标题】:'Response headers must contain header', u'location''响应标头必须包含标头', u'location'
【发布时间】:2023-03-16 16:15:01
【问题描述】:

我尝试使用可恢复上传在 Google Cloud Storage 存储桶中上传视频。 但我总是有同样的错误:(u'Response headers must contain header', u'location')

这是我的代码:

client = _get_storage_client()
bucket = client.bucket(BUCKET_NAME, PROJECT_ID)
blob = bucket.blob(filename)

if 'video' in content_type:
    url = blob.create_resumable_upload_session(content_type=content_type, client=client)
    stream = io.BytesIO(stream_file.file.read())


    upload = ResumableUpload(
            upload_url=url,
            chunk_size=chunk_size
        )
    transport = AuthorizedSession(credentials=client._credentials)

    # Start using the Resumable Upload
    response = upload.initiate(
            transport=transport,
            content_type=content_type,
            stream=stream,
            metadata={'name': blob.name}
        )
    while upload.finished is False:
        upload.transmit_next_chunk(transport)

upload.initiate()处出现错误

【问题讨论】:

    标签: python google-app-engine google-cloud-platform google-cloud-storage


    【解决方案1】:

    你的问题可能出在

    url = blob.create_resumable_upload_session(content_type=content_type, 
    client=client) 
    

    查看帖子here,他们使用

    # Create a Resumable Upload
    url = (
      f'https://www.googleapis.com/upload/storage/v1/b/'
      f'{bucket.name}/o?uploadType=resumable'
    )
    

    【讨论】:

      【解决方案2】:

      您的问题很可能与授权有关。这里的问题是这条线

      response = upload.initiate(
                  transport=transport,
                  content_type=content_type,
                  stream=stream,
                  metadata={'name': blob.name}
              )
      

      不包含谷歌云响应。 如果你进入这个语句,我会建议你调试这个语句,你会发现

              method, url, payload, headers = self._prepare_initiate_request(
                  stream, metadata, content_type,
                  total_bytes=total_bytes, stream_final=stream_final)
              result = _helpers.http_request(
                  transport, method, url, data=payload, headers=headers,
                  retry_strategy=self._retry_strategy)
              self._process_initiate_response(result)
              return result
      

      如果您检查“结果”变量。它将为您提供 HTTP 状态代码(403 表示未授权)。结果的内容将为您提供所需的原因和访问权限。

      另一种可能性是通过代理发送您的请求并检查 HTTP 结果。

      【讨论】:

        猜你喜欢
        • 2019-05-25
        • 2018-06-27
        • 2018-07-15
        • 2018-05-18
        • 2011-06-15
        • 1970-01-01
        • 2015-05-04
        • 1970-01-01
        相关资源
        最近更新 更多