如何检查Google Cloud Storage中是否存在文件? [英] How to check if file exists in Google Cloud Storage?

查看:310
本文介绍了如何检查Google Cloud Storage中是否存在文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,我想在其中检查存储桶中是否存在文件,如果不存在则创建一个文件.

I have a script where I want to check if a file exists in a bucket and if it doesn't then create one.

我尝试使用os.path.exists(file_path)其中file_path = "/gs/testbucket",但出现文件未找到错误.

I tried using os.path.exists(file_path) where file_path = "/gs/testbucket", but I got a file not found error.

我知道我可以使用files.listdir() API函数列出位于路径中的所有文件,然后检查我想要的文件是否为其中之一.但是我想知道是否还有另一种方法来检查文件是否存在.

I know that I can use the files.listdir() API function to list all the files located at a path and then check if the file I want is one of them. But I was wondering whether there is another way to check whether the file exists.

推荐答案

我想没有给定路径的直接检查文件是否存在的函数.
我创建了一个函数,该函数使用files.listdir() API函数列出存储桶中的所有文件,并将其与所需文件名进行匹配.如果找到则返回true,否则返回false.

I guess there is no function to check directly if the file exists given its path.
I have created a function that uses the files.listdir() API function to list all the files in the bucket and match it against the file name that we want. It returns true if found and false if not.

这篇关于如何检查Google Cloud Storage中是否存在文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆