简体   繁体   English

如何使用Python libcloud访问Google存储桶中的文件夹“ /”?

[英]How to access a folder “/” in the Google bucket using Python libcloud?

I have a Google bucket with the following tree ( Note the folder named "/"! ): 我有一个带有以下树的Google存储桶( 请注意名为“ /”!的文件夹 ):

"bucket-1"
   |
   |--- "data.csv"
   |
   |--- "/"
         |
         |--- "runs"
                 |
                 |--- "run-1"
                 |       |
                 |       |--- "data.csv"
                 |
                 |--- "run-2"
                         |
                         |--- "data.csv"

I want to access the objects (.csv files) using the Python library libcloud in the sub-folder "/". 我想使用子文件夹“ /”中的Python库libcloud访问对象(.csv文件)。

I can access data.csv which is outside of the "/" folder: 我可以访问“ /”文件夹之外的data.csv

>>> client.get_object(container_name='bucket-1', object_name='/data.csv')
<Object: name=/data.csv, size=181580, hash=8252d90d95b7b1cb7b4e699b90fbcce3, provider=Google Cloud Storage ...>

Using gsutil with two slashes I can see objects in "/": 使用带有两个斜杠的gsutil,我可以在“ /”中看到对象:

>>> gsutil ls "gs://bucket-1//runs/run-1"
gs://bucket-1//runs/run-1/data.csv

However with libcloud if I do client.get_object(container_name='bucket-1', object_name='//runs/run-1/data.csv') or client.get_object(container_name='bucket-1', object_name='/runs/run-1/data.csv') I get the error: 但是使用libcloud如果我做client.get_object(container_name='bucket-1', object_name='//runs/run-1/data.csv')client.get_object(container_name='bucket-1', object_name='/runs/run-1/data.csv')我收到错误消息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/andrey/miniconda3/envs/mostly-cloud/lib/python3.6/site-packages/libcloud/storage/drivers/s3.py", line 342, in get_object
    object_name=object_name)
libcloud.storage.types.ObjectDoesNotExistError: <ObjectDoesNotExistError in <libcloud.storage.drivers.google_storage.GoogleStorageDriver object at 0x7f40560cd4e0>, value=None, object = //runs/run-1/data.csv>

On the other hand, 另一方面,

client.list_container_objects(client.get_container("bucket-1"))
[<Object: name=/runs/run-1/data.csv, size=357683, hash=..., provider=Google Cloud Storage ...>, <Object: name=/runs/run-2/data.csv, size=357683, hash=..., provider=Google Cloud Storage ...>] 

So, how to get an object located in the "/" directory? 那么,如何获取位于“ /”目录中的对象?

I reproduced your scenario in order to test this behavior. 我转载了您的情况以测试此行为。 I was able to create this hierarchy through: 我能够通过以下方式创建此层次结构:

gsutil cp your-file gs://your-bucket//abc

This is a weird behavior and it shouldn't be allowed. 这是一个怪异的行为,不应允许。

If you try to create a folder with this name from the GCP console, you will see the message: 如果您尝试从GCP控制台使用此名称创建文件夹,则会看到以下消息:

Forward slashes (/) are not allowed in folder names. 文件夹名称中不允许使用正斜杠(/)。

For that reason I created a Public Issue Tracker where you can get feedback regarding this issue. 因此,我创建了一个公共问题跟踪器 ,您可以在其中获取有关此问题的反馈。

About the naming of your folders, you can take a look at Google's documentation about how subdirectories are working. 关于文件夹的命名,您可以查看有关子目录如何工作的Google文档。

To sum up, you shouldn't be allowed to create a folder with this name. 综上所述,不应允许您使用此名称创建文件夹。 The best course of action now is to avoid names like this and prefer a string name which you will be able to handle. 现在最好的做法是避免使用类似这样的名称,而希望使用能够处理的字符串名称。

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

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