简体   繁体   English

Golang - 从共享的 Google 云端硬盘文件夹中获取文件列表

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

I am trying to list files and their IDs from a Google Drive folder using a known folder ID.我正在尝试使用已知文件夹 ID 列出 Google 云端硬盘文件夹中的文件及其 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编辑
Thanks to @Tanaike's answer, I learned that I was confused about some points.感谢@Tanaike 的回答,我了解到我对某些要点感到困惑。 I am not attempting to get a list of files from a shared Drive, but a shared folder.我不是要从共享驱动器获取文件列表,而是要从共享文件夹获取文件列表。
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.所以,我认为权限是可以的。
Here is how I am now trying to get the list (driveService is set up the same as above)这是我现在尝试获取列表的方式(driveService 的设置与上面相同)

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

I still get the panic and signal.我仍然得到恐慌和信号。

I believe your goal is as follows.我相信你的目标如下。

  • You want to retrieve the file list from the shared drive using googleapis for go.您想要使用 go 的 googleapis 从共享驱动器中检索文件列表。

In this case, how about the following modification?在这种情况下,如何进行以下修改?

From:从:

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

To:到:

r, err := driveService.Files.List().
    DriveId("XXXXXXXXXX").
    Corpora("drive").
    SupportsAllDrives(true).
    IncludeItemsFromAllDrives(true).
    PageSize(10).
    Fields("nextPageToken, files(id, name)").
    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.本次修改假设您有读取共享盘的权限,并且您已经可以使用API盘,请注意这一点。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何列出未与组织共享的 Google Drive 文件 - How to list Google Drive files not shared with the organisation 从Google云端硬盘下载公共文件 - Golang - Download public file from Google Drive - Golang 如何使用 Google Drive API 列出域中所有共享驱动器中的文件? - How to list files inside all Shared Drives in a domain with Google Drive API? 通过 Golang API 上传大文件到 Google Drive - Upload large files to Google Drive via Golang API 更改 Google 共享驱动器的“共享驱动器设置” - Change "Shared drive settings" of Google Shared Drive 如何使用 Google Drive API v3 在共享驱动器中按 ID 检索文件夹 - How to retrieve folder by ID in a shared drive with Google Drive API v3 尝试从 golang 广告读取/运行对 bigquery 的查询获取拒绝访问:BigQuery BigQuery:未找到具有 Google Drive 范围的 OAuth 令牌 - Trying to read/run query on bigquery from golang ad get Access Denied: BigQuery BigQuery: No OAuth token with Google Drive scope was found 如何使用golang从Amazon S3存储桶的子文件夹中获取对象列表? - How to get list of objects from sub folder of Amazon S3 bucket using golang? 如何使用 Google Drive API(v3) 获取目录中所有文件的列表 - How to get a list of all files in directory with Google Drive API(v3) 如何判断文件夹是否与 GoLang 或 CMD 共享? - How to tell if a folder is shared with GoLang or CMD?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM