GCS存储桶如何使用过滤器获取对象 [Golang]

huangapple go评论85阅读模式
英文:

gcs bucket how to get objects using filters [golang]

问题

有没有一种方法可以获取以特定条件开头和结尾的对象列表呢?

例如:

a/b/c/id1.json
a/b/c/id2.json
a/b/c/id3.json
a/c/id1.json

我们想要查询:

前缀: "a/",
结束位置: "id1.json"

期望的输出应该是:

a/b/c/id1.json

我们想要过滤掉其他选项,但我们不知道b文件夹的名称是什么。

所以:

a始终是a

b是随机唯一的字符串

c始终是c

我们总是想要特定的json文件。

我正在尝试使用以下代码实现:

query := &storage.Query{
	Prefix: "a/",
	//StartOffset: "",
	EndOffset: "id1.json",
    //Delimiter:
}
query.SetAttrSelection([]string{"Name"})

或者

Prefix:                   "c/id1.json",
Delimiter:                "/",
IncludeTrailingDelimiter: true,

但是由于某种原因,我得到了所有这些文件的返回结果。

当然,我希望尽可能地限制结果以提高性能。

也许有办法在Prefix定义中使用一些正则表达式?像a/*/c/id1.json这样的。

谢谢。

英文:

Is there a way to get list of objects which starts and ends with specific criteria
as example:

a/b/c/id1.json
a/b/c/id2.json
a/b/c/id3.json
a/c/id1.json

and we wanna query for

Prefix: "a/",
EndOffset: "id1.json"

expected output should be:

a/b/c/id1.json

and we wanna filter out other options and we don't know what the b folder name would be.

So:

a is always a

b is random uniq string

c is always c

and we always want the specific json.

As i am tying to achieve this with:

query := &storage.Query{
		Prefix: "a/",
		//StartOffset: "",
		EndOffset: "id1.json",
        //Delimiter:
}
query.SetAttrSelection([]string{"Name"})

or

Prefix:                   "c/id1.json",
Delimiter:                "/",
IncludeTrailingDelimiter: true,

and for some reason i am getting in return all of those files.

And of course i would like to limit the results as much as possible for better performances.

Maybe there is a way to use some regex in Prefix definition ?
like a/*/c/id1.json

Thanks

----------------------- ========= Edited ========= -----------------------

Please note that this is already implemented by me storage_list_files_with_prefix-go and do not work as i would like to have it. So the main question is HOWTO make this filtering working with the example I am showing.

答案1

得分: 1

关键要点:

  • 云存储桶没有目录。
  • 命名空间是扁平的。
  • 对象名称只是字符串。
  • 斜杠/字符通常用于文件系统中分隔目录名称,但在云存储桶对象名称中只是一个字符。斜杠没有特殊意义,但可以用作分隔符。
  • 您可以指定前缀和分隔符来减少返回的对象列表。
  • 云存储不支持正则表达式。
  • 星号*是一个字符,而不是通配符。

总结:

  • 您必须在代码中实现额外的过滤。
英文:

Key points:

  • Cloud Storage Buckets do not have directories.
  • The namespace is flat.
  • Object names are just strings.
  • The slash / character which is often used to separate directory names in file systems is just a character in an Bucket object name. The slash has no significance but can be used as a delimiter.
  • You can specify a prefix and a delimiter to reduce the returned object list.
  • Cloud Storage does not support regex expressions.
  • The asterisk * is a character and not a wildcard.

Summary:

  • You must implement additional filtering in your code.

List the objects in a bucket using a prefix filter

huangapple
  • 本文由 发表于 2022年9月10日 13:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/73669544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定