[英]Listing files in google cloud storage in python 2.7
如果你确切地知道你想要的路径,你可以使用来自云存储的 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.