有没有适用于Google Cloud Storage的Go实现的fs.ReadDir函数?

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

Is there a Go implementation of fs.ReadDir for Google Cloud Storage?

问题

我正在使用Go语言创建一个Web应用程序。

我修改了我的工作代码,使其可以根据标志位在本地文件系统和Google Cloud Storage的存储桶中读写文件。

基本上,我在中间包中包含了一个小的包,并实现了my-own-pkg.readFile或my-own-pkg.WriteFile等方法...

我已经用我的方法替换了代码中所有读取或保存本地文件的调用。

最后,这些方法包括一个简单的switch case语句,用于运行标准代码以进行本地读写或从/到gcp存储桶的读写。

我目前的问题
在某些部分,我需要执行ReadDir来获取DirEntries的列表,然后遍历它们。除了将os.readDir替换为my-own-pkg.ReadDir之外,我不想改变我的代码。

到目前为止,我了解到gcp模块中没有原生的函数。所以我猜测(但我需要你的帮助,因为我只是猜测),我需要一个适用于gcp的fs.FS的实现。由于它是go 1.6的新功能,我猜现在找到一个实现可能还为时过早。

所以我试图简单地创建一个my-own-pkg.ReadDir(folderpath)函数,它执行以下操作:
1)case "local": { <像往常一样读取目录>}
2)case "gcp": {

<使用gcp代码示例列出我的存储桶中具有Query.Prefix = folderpath和Query.Delimiter = "/"的对象

然后创建一个my-own-pkg.DirEntry的切片(因为fs.DirEntry只是一个接口,所以它需要被实现... 有没有适用于Google Cloud Storage的Go实现的fs.ReadDir函数? )并返回它们。

为了做到这一点,我还需要实现fs.DirEntry接口(这需要实现FileInfo接口和可能还有其他一些接口...)

**问题1)**这是解决我的问题的正确方法吗?还是有更好的方法?

问题2)(仅限)如果是这样,使用前缀和分隔符列出对象的gcp方法是否只返回文件?我看不到返回找到的前缀列表的方法

(如果我有前缀/file1.txt和前缀/a/file2.txt,我希望同时得到"file1.txt"和"a"作为文件和前缀...)

希望我表达清楚了...这次我不能包含代码,因为它是不完整的...但是如果有帮助的话,我可以粘贴我能提供的部分。

注意:顺便说一句,通过现有的fs.FS实现和相关的ReadDirFS,go 1.6使我能够优雅地解决处理嵌入或文件系统上的资源时遇到的类似问题。所以如果我能按照相同的路线走就太好了 🙂

顺便说一句,我正在继续学习和实验,所以如果成功的话,我也会做出贡献的 有没有适用于Google Cloud Storage的Go实现的fs.ReadDir函数?

英文:

I am creating a web application in Go.

I have modified my working code so that it can read and write files on both a local filesystem and a bucket of Google Cloud Storage based on a flag.

Basically I included a small package in the middle, and I implemented my-own-pkg.readFile or my-own-pkg.WriteFile and so on...

I have replaced all calls in my code where I read or save files from the local filesystem with calls to my methods.

Finally these methods include a simple switch case that runs the standard code to read/write locally or the code to read/wrote from/to a gcp bucket.

My current problem
In some parts I need to perform a ReadDir to get the list of DirEntries and then cycle though them. I do not want to change my code except for replacing os.readDir with my-own-pkg.ReadDir.

So far I understand that there is not a native function in the gcp module. So I suppose (but here I need your help because I am just guessing) that I would need an implementation of fs.FS for the gcp. It being a new feature of go 1.6 I guess it's too early to find one.

So I am trying to create simply a my-own-pkg.ReadDir(folderpath) function that does the following:

  1. case "local": { <readdir as usual>}
  2. case "gcp": {

<Use gcp code sample to list objects in my bucket with Query.Prefix = folderpath and
Query.Delimiter="/"

Then create a slice of my-own-pkg.DirEntry (because fs.DkrEntry is just an interface and so it needs to be implemented... 有没有适用于Google Cloud Storage的Go实现的fs.ReadDir函数? ) and return them.

In order to do so I need to implement also the interface fs.DirEntry (which requires the implementation of interface for FileInfo and maybe something else...)

Question 1) is this the right path to follow to solve my issue or is there a better way?

Question 2) (only) if so, does the gcp method that lists object with a prefix and a delimiter return just files? I can't see a method that returns also the list of prefixes found

(If I have prefix/file1.txt and prefix/a/file2.txt I would like to get both "file1.txt" and "a" as files and prefixes...)

I hope I was enough clear... This time I can't include code because it's incomplete... But in case it helps I can paste what I can.

NOTE: by the way go 1.6 allowed me to solve elegantly a similar issue when dealing with assets either embedded or on the filesystem thanks to the existing implementation of fs.FS and the related ReadDirFS. So good if I could follow the same route 🙂

By the way I am going on studying and experimenting so in case I am successful I will contribute as well 有没有适用于Google Cloud Storage的Go实现的fs.ReadDir函数?

答案1

得分: 1

我认为你的抽象层很好,但你需要了解一些关于云存储的内容:目录不存在。

实际上,所有的对象都放在存储桶的根目录/下,对象的完全限定名称是/path/to/object.file。你可以根据前缀进行过滤,返回所有具有相同路径前缀的对象(即文件,因为目录不存在)。

这并不是对你问题的完整回答,但我相信你可以考虑并重新设计剩下的代码,考虑到这个特点。

英文:

I think your abstraction layer is good but you need to know something on Cloud Storage: The directory doesn't exist.

In fact, all the object are put at the root of the bucket / and the fully qualified name of the object is /path/to/object.file. You can filter on a prefix, that return all the object (i.e. file because directory doesn't exist) with the same path prefix.

It's not a full answer to your question but I'm sure that you can think and redesign the rest of your code with this particularity in mind.

huangapple
  • 本文由 发表于 2021年5月30日 16:26:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/67759198.html
匿名

发表评论

匿名网友

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

确定