1. <legend id='NUcY7'><style id='NUcY7'><dir id='NUcY7'><q id='NUcY7'></q></dir></style></legend>

    <small id='NUcY7'></small><noframes id='NUcY7'>

    <i id='NUcY7'><tr id='NUcY7'><dt id='NUcY7'><q id='NUcY7'><span id='NUcY7'><b id='NUcY7'><form id='NUcY7'><ins id='NUcY7'></ins><ul id='NUcY7'></ul><sub id='NUcY7'></sub></form><legend id='NUcY7'></legend><bdo id='NUcY7'><pre id='NUcY7'><center id='NUcY7'></center></pre></bdo></b><th id='NUcY7'></th></span></q></dt></tr></i><div id='NUcY7'><tfoot id='NUcY7'></tfoot><dl id='NUcY7'><fieldset id='NUcY7'></fieldset></dl></div>
    <tfoot id='NUcY7'></tfoot>

      • <bdo id='NUcY7'></bdo><ul id='NUcY7'></ul>

      Google Cloud Storage + Python:有什么方法可以在 GCS 的某个文件夹中列出 obj?

      Google Cloud Storage + Python : Any way to list obj in certain folder in GCS?(Google Cloud Storage + Python:有什么方法可以在 GCS 的某个文件夹中列出 obj?)
      • <i id='AKPys'><tr id='AKPys'><dt id='AKPys'><q id='AKPys'><span id='AKPys'><b id='AKPys'><form id='AKPys'><ins id='AKPys'></ins><ul id='AKPys'></ul><sub id='AKPys'></sub></form><legend id='AKPys'></legend><bdo id='AKPys'><pre id='AKPys'><center id='AKPys'></center></pre></bdo></b><th id='AKPys'></th></span></q></dt></tr></i><div id='AKPys'><tfoot id='AKPys'></tfoot><dl id='AKPys'><fieldset id='AKPys'></fieldset></dl></div>
            <tbody id='AKPys'></tbody>
        • <tfoot id='AKPys'></tfoot>

          <small id='AKPys'></small><noframes id='AKPys'>

            <bdo id='AKPys'></bdo><ul id='AKPys'></ul>

              <legend id='AKPys'><style id='AKPys'><dir id='AKPys'><q id='AKPys'></q></dir></style></legend>

                本文介绍了Google Cloud Storage + Python:有什么方法可以在 GCS 的某个文件夹中列出 obj?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我要编写一个 Python 程序来检查文件是否在我的 Google Cloud Storage 的某个文件夹中,基本思想是获取文件夹中所有对象的 list,a文件名list,然后检查文件abc.txt是否在文件名list中.

                I'm going to write a Python program to check if a file is in certain folder of my Google Cloud Storage, the basic idea is to get the list of all objects in a folder, a file name list, then check if the file abc.txt is in the file name list.

                现在的问题是,看起来谷歌只提供了一种获取obj list的方法,即uri.get_bucket(),请参阅以下代码,该代码来自 https://developers.google.com/storage/docs/gspythonlibrary#listing-objects

                Now the problem is, it looks Google only provide the one way to get obj list, which is uri.get_bucket(), see below code which is from https://developers.google.com/storage/docs/gspythonlibrary#listing-objects

                uri = boto.storage_uri(DOGS_BUCKET, GOOGLE_STORAGE)
                for obj in uri.get_bucket():
                    print '%s://%s/%s' % (uri.scheme, uri.bucket_name, obj.name)
                    print '  "%s"' % obj.get_contents_as_string()
                

                uri.get_bucket() 的缺陷是,它看起来是先获取所有对象,这是我不想要的,我只需要获取 obj name list of specific folder(eg gs//mybucket/abc/myfolder) ,应该很快.

                The defect of uri.get_bucket() is, it looks it is getting all of the object first, this is what I don't want, I just need get the obj name list of particular folder(e.g gs//mybucket/abc/myfolder) , which should be much quickly.

                有人可以帮忙解答吗?感谢每一个答案!

                Could someone help answer? Appreciate every answer!

                推荐答案

                更新:以下适用于 Python 的旧版Google API 客户端库",但如果您不使用它客户端,更喜欢 Python 的较新的Google Cloud 客户端库"(https://googleapis.dev/python/storage/latest/index.html ).对于较新的库,等效于以下代码:

                Update: the below is true for the older "Google API Client Libraries" for Python, but if you're not using that client, prefer the newer "Google Cloud Client Library" for Python ( https://googleapis.dev/python/storage/latest/index.html ). For the newer library, the equivalent to the below code is:

                from google.cloud import storage
                
                client = storage.Client()
                for blob in client.list_blobs('bucketname', prefix='abc/myfolder'):
                  print(str(blob))
                

                老客户的回答如下.

                您可能会发现使用 JSON API 更容易,它有一个功能齐全的 Python 客户端.它有一个用于列出带有前缀参数的对象的功能,您可以使用它以这种方式检查某个目录及其子目录:

                You may find it easier to work with the JSON API, which has a full-featured Python client. It has a function for listing objects that takes a prefix parameter, which you could use to check for a certain directory and its children in this manner:

                from apiclient import discovery
                
                # Auth goes here if necessary. Create authorized http object...
                client = discovery.build('storage', 'v1') # add http=whatever param if auth
                request = client.objects().list(
                    bucket="mybucket",
                    prefix="abc/myfolder")
                while request is not None:
                  response = request.execute()
                  print json.dumps(response, indent=2)
                  request = request.list_next(request, response)
                

                列表调用的更完整文档在这里:https://developers.google.com/storage/docs/json_api/v1/objects/list

                Fuller documentation of the list call is here: https://developers.google.com/storage/docs/json_api/v1/objects/list

                Google Python API 客户端记录在这里:https://code.google.com/p/google-api-python-客户/

                And the Google Python API client is documented here: https://code.google.com/p/google-api-python-client/

                这篇关于Google Cloud Storage + Python:有什么方法可以在 GCS 的某个文件夹中列出 obj?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                • <bdo id='bt7DU'></bdo><ul id='bt7DU'></ul>
                    <tbody id='bt7DU'></tbody>

                  <small id='bt7DU'></small><noframes id='bt7DU'>

                  <tfoot id='bt7DU'></tfoot>
                    1. <legend id='bt7DU'><style id='bt7DU'><dir id='bt7DU'><q id='bt7DU'></q></dir></style></legend>

                        • <i id='bt7DU'><tr id='bt7DU'><dt id='bt7DU'><q id='bt7DU'><span id='bt7DU'><b id='bt7DU'><form id='bt7DU'><ins id='bt7DU'></ins><ul id='bt7DU'></ul><sub id='bt7DU'></sub></form><legend id='bt7DU'></legend><bdo id='bt7DU'><pre id='bt7DU'><center id='bt7DU'></center></pre></bdo></b><th id='bt7DU'></th></span></q></dt></tr></i><div id='bt7DU'><tfoot id='bt7DU'></tfoot><dl id='bt7DU'><fieldset id='bt7DU'></fieldset></dl></div>