Golang – 从共享的Google Drive文件夹获取文件列表

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

Golang - get list of files from a shared Google Drive folder

问题

我正在尝试使用已知的文件夹ID从Google Drive文件夹中列出文件及其ID。

设置驱动服务似乎成功了。但是,在尝试使用信息填充变量时,我遇到了一个错误。以下是代码片段:

driveService, err := drive.NewService(ctx, option.WithCredentialsFile("../serverFiles/credentials.json"))
if err != nil {
    log.Printf("无法检索Drive客户端:%v", err)
}

r, err := driveService.Files.List().
    DriveId("XXXXXXXXXX").
    PageSize(10).
    Fields("nextPageToken, files(id, name)").
    Do()
if err != nil {
    log.Fatalf("无法检索文件:%v", err)
}

这是我收到的错误:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x14312db]

感谢任何帮助。

编辑
感谢@Tanaike的答案,我了解到我对某些问题感到困惑。
我不是尝试从共享驱动器获取文件列表,而是从共享文件夹获取。
我正在使用服务帐号进行访问。在另一个脚本中,相同的服务帐号能够写入相同的驱动器文件夹。所以,我认为权限是正确的。
以下是我现在尝试获取列表的方式(driveService的设置与上面相同):

r, err := driveService.Files.List().
    Corpora("user").
    PageSize(10).
    Q("'XXXXXXXX' in parents").
    Fields("nextPageToken, files(id, name)").
    Do()

我仍然遇到了panic和signal错误。

英文:

I am trying to list files and their IDs from a Google Drive folder using a known folder ID.

The drive service set up seems to be successful. But, I get a panic attempting to populate a variable with the info. Here is a snippet of the code:

	driveService, err := drive.NewService(ctx, option.WithCredentialsFile("../serverFiles/credentials.json"))
    if err != nil {
	    log.Printf("Unable to retrieve Drive client: %v", err)
    }

	r, err := driveService.Files.List().
    	DriveId('XXXXXXXXXX').
	    PageSize(10).
    	Fields("nextPageToken, files(id, name)").
	    Do()
    if err != nil {
	    log.Fatalf("Unable to retrieve files: %v", err)
    }

Here is the error I get:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x14312db]

Thanks for any help.

EDIT<br>
Thanks to @Tanaike's answer, I learned that I was confused about some points.
I am not attempting to get a list of files from a shared Drive, but a shared folder. <br>
I am using a service account to access. In another script the same service account is able to write to this same drive folder. So, I think permissions are OK. <br>
Here is how I am now trying to get the list (driveService is set up the same as above)

	r, err := driveService.Files.List().
		Corpora(&quot;user&quot;).
    	PageSize(10).
	    Q(&quot;&#39;XXXXXXXX&#39; in parents&quot;).
	    Fields(&quot;nextPageToken, files(id, name)&quot;).
	    Do()

I still get the panic and signal.

答案1

得分: 2

我相信你的目标如下。

  • 你想使用googleapis for go从共享驱动器中检索文件列表。

在这种情况下,以下修改如何?

从:

r, err := driveService.Files.List().
    DriveId('XXXXXXXXXX').
    PageSize(10).
    Fields("nextPageToken, files(id, name)").
    Do()

到:

r, err := driveService.Files.List().
	DriveId("XXXXXXXXXX").
	Corpora("drive").
	SupportsAllDrives(true).
	IncludeItemsFromAllDrives(true).
	PageSize(10).
	Fields("nextPageToken, files(id, name)").
	Do()

注意:

  • 在这个修改中,假设你有读取共享驱动器的权限,并且你已经能够使用Drive API。请注意这一点。

参考:

英文:

I believe your goal is as follows.

  • You want to retrieve the file list from the shared drive using googleapis for go.

In this case, how about the following modification?

From:

r, err := driveService.Files.List().
    DriveId(&#39;XXXXXXXXXX&#39;).
    PageSize(10).
    Fields(&quot;nextPageToken, files(id, name)&quot;).
    Do()

To:

r, err := driveService.Files.List().
	DriveId(&quot;XXXXXXXXXX&quot;).
	Corpora(&quot;drive&quot;).
	SupportsAllDrives(true).
	IncludeItemsFromAllDrives(true).
	PageSize(10).
	Fields(&quot;nextPageToken, files(id, name)&quot;).
	Do()

Note:

  • In this modification, it supposes that you have permission for reading the shared drive and you have already been able to use Drive API. Please be careful about this.

Reference:

huangapple
  • 本文由 发表于 2022年3月9日 05:56:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/71401942.html
匿名

发表评论

匿名网友

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

确定