繁体   English   中英

在python 2.7中列出谷歌云存储中的文件

[英]Listing files in google cloud storage in python 2.7

我在谷歌 API 引擎中有一个项目,我使用谷歌云存储在一个任务中保存一些文件,并在另一个任务中列出其中的一些文件来处理它们。 我在这里找不到任何答案( 是在 php 中, 这是在 java 中,两者都不是很有帮助, 似乎是 python3)或外部。 所以我想要的是这样的:

import cloudstorage

files = cloudstorage.list('/bucket/foo/bar')
for file in files:
  # process files

如果你确切地知道你想要的路径,你可以使用来自云存储的 listbucket 因此,要列出“/bucket/foo/bar”中的所有文件,请使用:

import cloudstorage

files = cloudstorage.listbucket('/bucket/foo/bar')
for file in files:
  file_name = file.filename
  # process file

listbucket返回的迭代器中的每个文件都是GCSFileStat实例

您还可以按文件名的开头过滤文件,例如,列出上一个文件夹中以“baz”开头的每个文件:

files = cloudstorage.listbucket('/bucket/foo/bar/baz')

或者列出test.json之后的所有文件:

files = cloudstorage.listbucket('/bucket/foo/bar/test.json')

要在 unittest 中使用这些,您需要从 testbed 设置 blobstore 和 urlfect 存根:

import unittest
from google.appengine.ext import testbed

class CloudStorageTestCase(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.testbed = testbed.Testbed()
        cls.testbed.activate()

        cls.testbed.init_blobstore_stub()
        cls.testbed.init_urlfetch_stub()

在我的测试中,您需要 urlfetch 和 blobstore 存根才能在测试中使用云存储。 是整个测试文件的要点

暂无
暂无

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

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