我正在使用一个库 (Apache Libcloud) 向谷歌存储发出请求。
在库内部,向在谷歌存储中查询的 URL 发出 HEAD 请求。
您可以找到它的代码 here,搜索 def get_object
。
重要的一行是
response = self.connection.request(object_path, method='HEAD')
根据 Google Documentation of HEAD response objects,content-length
字段应该是响应的一部分:
Response
...
expires: Wed, 30 May 2018 21:41:23 GMT
date: Wed, 30 May 2018 21:41:23 GMT
cache-control: private, max-age=0
last-modified: Wed, 30 May 2018 20:36:34 GMT
etag: "2218880ef78838266ecd7d4c1b742a0e"
x-goog-generation: 1486161811706000
x-goog-metageneration: 15
x-goog-stored-content-encoding: identity
x-goog-stored-content-length: 328
content-type: image/jpg
x-goog-hash: crc32c=HBrbzQ==
x-goog-hash: md5=OCydg52+pPG1Bwawjsl7DA==
x-goog-storage-class: STANDARD
accept-ranges: bytes
content-length: 328 # <--- here
...
但是,某些文件(但并非所有文件)缺少它。
在这两种情况下,我都收到了 x-goog-stored-content-length
条目,但图书馆需要 content-length
。
content-length
标头在 def _headers_to_object
中的调用链下游使用了一点,由于缺少标头,我在那里得到了 KeyError:
def _headers_to_object(self, object_name, container, headers):
hash = headers['etag'].replace('"', '')
extra = {'content_type': headers['content-type'],
'etag': headers['etag']}
meta_data = {}
if 'last-modified' in headers:
extra['last_modified'] = headers['last-modified']
for key, value in headers.items():
if not key.lower().startswith(self.http_vendor_prefix + '-meta-'):
continue
key = key.replace(self.http_vendor_prefix + '-meta-', '')
meta_data[key] = value
obj = Object(name=object_name, size=headers['content-length'], # <-- here
hash=hash, extra=extra,
meta_data=meta_data,
container=container,
driver=self)
return obj
问题是: 当我上传文件导致谷歌存储不发送该标头时,我可能对文件做了什么? 或者这是谷歌存储中的错误(我怀疑)?
更多信息:
application/json
content-length
标头,但如果我覆盖 _headers_to_object
以使用 x-goog-stored-content-length
,它会起作用。