简体   繁体   English

使用 Apache Libcloud 上传到 GCS 时,Object 元数据密钥为小写

[英]Object metadata keys are lowercased when uploading to GCS with Apache Libcloud

I'm using Apache Libcloud to upload files to a Google Cloud Storage bucket together with object metadata.我正在使用 Apache Libcloud 将文件与 object 元数据一起上传到 Google Cloud Storage 存储桶。

In the process, the keys in my metadata dict are being lowercased.在此过程中,我的元数据字典中的键被小写。 I'm not sure whether this is due to Cloud Storage or whether this happens in Libcloud.我不确定这是由于 Cloud Storage 还是发生在 Libcloud 中。

The issue can be reproduced following the example from the Libcloud docs :可以按照Libcloud 文档中的示例重现该问题:

from libcloud.storage.types import Provider
from libcloud.storage.providers import get_driver

cls = get_driver(Provider.GOOGLE_STORAGE)
driver = cls('SA-EMAIL', './SA.json') # provide service account credentials here


FILE_PATH = '/home/user/file'

extra = {'meta_data': {'camelCase': 'foo'}}

# Upload with metadata
with open(FILE_PATH, 'rb') as iterator:
    obj = driver.upload_object_via_stream(iterator=iterator,
                                          container=container,
                                          object_name='file',
                                          extra=extra)

The file uploads succesfully, but resulting metadata will look as follows:文件成功上传,但生成的元数据将如下所示: 结果

Where camelCase has been turned into camelcase . camelCase已变成camelcase的地方。

I don't think GCS disallows camelcase for object metadata, since it's possible to edit the metadata manually in that sense:我不认为 GCS 不允许 object 元数据的驼峰式,因为在这个意义上可以手动编辑元数据: 在此处输入图像描述

I went through Libcloud's source code, but I don't see any explicit lowercasing going on.我浏览了 Libcloud 的源代码,但没有看到任何明确的小写字母。 Any pointers on how to upload camelcased metadata with libcloud are most welcome.任何关于如何使用 libcloud 上传驼峰元数据的指示都非常受欢迎。

I also checked the library and wasn't able to see anything obvious.我还检查了图书馆,没有看到任何明显的东西。 But I guess to open a new issue there will be a great start.但我想打开一个新问题会有一个很好的开始。

As far as what's concerned on the Google Cloud Storage side, and as you could verify by yourself it does admit camelcase.至于谷歌云存储方面的问题,你可以自己验证它确实承认驼峰式。 I was able to successfully edit the metadata of a file by using the code offered on their public docs (but wasn't able to figure out something on libcloud itself):通过使用他们的公共文档上提供的代码,我能够成功地编辑文件的元数据(但无法在 libcloud 本身上找出一些东西):

from google.cloud import storage


def set_blob_metadata(bucket_name, blob_name):
    """Set a blob's metadata."""
    # bucket_name = 'your-bucket-name'
    # blob_name = 'your-object-name'

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.get_blob(blob_name)
    metadata = {'camelCase': 'foo', 'NaMe': 'TeSt'}
    blob.metadata = metadata
    blob.patch()

    print("The metadata for the blob {} is {}".format(blob.name, blob.metadata))

So, I believe that this could be a good workaround on your case if you are not able to work it out with libcloud.因此,如果您无法使用 libcloud 解决问题,我相信这可能是您的情况的一个很好的解决方法。 Do notice that the Cloud Storage Client Libraries base their authentication on environment variables and the following docs should be followed .请注意,云存储客户端库的身份验证基于环境变量, 应遵循以下文档

Addition by question author : As hinted at in the comments, metadata can be added to a blob before uploading a file as follows:问题作者添加:正如评论中所暗示的,可以在上传文件之前将元数据添加到 blob,如下所示:

from google.cloud import storage
gcs = storage.Client()
bucket = gcs.get_bucket('my-bucket')
blob = bucket.blob('document')
blob.metadata = {'camelCase': 'foobar'}
blob.upload_from_file(open('/path/to/document', 'rb'))

This allows to set metadata without having to patch an existing blob, and provides an effective workaround for the issue with libcloud.这允许在无需修补现有 blob 的情况下设置元数据,并为 libcloud 的问题提供有效的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM