如何根据正则表达式从Google Cloud Storage存储桶中获取文件? [英] How to get files from Google Cloud Storage bucket based on a regular expression?

查看:129
本文介绍了如何根据正则表达式从Google Cloud Storage存储桶中获取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Google云存储桶中获取文件.文件名类似于20180618_1400/SOMEID_20180618.jpg20180618_1200/SOMEID_20180618.jpg20180617_1400/SOMEOTHERID_20180617.jpg等.

I am trying to get files from a google cloud storage bucket. The file name are something like 20180618_1400/SOMEID_20180618.jpg, 20180618_1200/SOMEID_20180618.jpg, 20180617_1400/SOMEOTHERID_20180617.jpg, etc.

我想获取基于SOMEID的文件. 我尝试将以下代码与reg exp

I want to get files based on SOMEID. I tried using the following code with reg exp

bucket.getFiles({ prefix: new RegExp(`[0-9_]*\/SOMEID_`), }, (err, files) => { if (err) return reject(err); resolve(files); });

bucket.getFiles({ prefix: new RegExp(`[0-9_]*\/SOMEID_`), }, (err, files) => { if (err) return reject(err); resolve(files); });

预期结果是文件20180618_1400/SOMEID_20180618.jpg20180618_1200/SOMEID_20180618.jpg.但是代码返回了存储桶中的所有文件.

The expected result is files 20180618_1400/SOMEID_20180618.jpg and 20180618_1200/SOMEID_20180618.jpg. But the code returns all the files in the bucket.

我在互联网上搜索,但找不到任何内容.

I searched on the internet but couldn't find anything.

还有其他方法可以实现这一目标吗?

Is there any other way to achieve this?

推荐答案

前缀必须是字符串.这是一个前缀,而不是正则表达式.我希望确保在文档中 ,并且按预期,这是不可能的.

The prefix has to be a string. This is a prefix, not a regex. I had a look to be sure in documentation and it is, as expected, not possible.

在GCS中执行此操作的正确方法是使用字符串作为前缀来构造存储桶.例如,有一个用于个人资料图片的目录,另一个用于pdf目录,...所有文件都用您的用户ID命名.

The correct way to do that in GCS would be to structure your bucket in a way prefix as a string is usable. For example, having a directory for profile picture, another for pdf, ... And all files are named with your user id.

示例:

profiles/1245.jpg
profiles/7561.jpg
billing/1245-2018-10.pdf
billing/1245-2018-09.pdf
billing/7561-2018-10.pdf
...

如果不能,则必须获取所有项目,然后在其上应用正则表达式.您在getFiles()文档末尾有一个示例

If you cannot, you will have to get all items and then apply your regex on it. You have an example at the end of the getFiles() documentation

我认为(已经有一段时间了),您可以使用gsutils使用正则表达式,但是gsutils会获取所有文件,然后将正则表达式应用到客户端,所以这不是一个更好的解决方案.

I think (it's been a while), you can use a regex using gsutils, but gsutils get all files and then apply the regex on the client side, so it won't be a better solution.

这篇关于如何根据正则表达式从Google Cloud Storage存储桶中获取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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