Skip to content

Instantly share code, notes, and snippets.

@KireinaHoro
Last active May 4, 2021 16:11
Show Gist options
  • Save KireinaHoro/dd40f5a2c651a716973496e08df2a358 to your computer and use it in GitHub Desktop.
Save KireinaHoro/dd40f5a2c651a716973496e08df2a358 to your computer and use it in GitHub Desktop.
Query Google Drive files with Golang
// slice of dictates for query
var q []string
//q = append(q, fmt.Sprintf("mimeType='%s'", driveFolderType))
//q = append(q, "name='Uncategorized'")
//q = append(q, "('0B9P4nJCkns_fWERqT3dfVENHMXM' in parents)")
q = append(q, "trashed=false")
q = append(q, "(name contains 'VCB')")
// construct the call for listing the requested files
listCall := srv.Files.List().PageSize(10).
Fields("nextPageToken, files(id, name, parents, trashed)").Q(strings.Join(q, "and"))
// retrieve the file list
fmt.Println("Files:")
var pageToken string
// max number of pages to load
maxPage := 10
for i := 1; i <= maxPage; i ++ {
r, err := listCall.PageToken(pageToken).Do()
if err != nil {
log.Fatalf("An error occurred: %v", err)
}
if len(r.Files) > 0 {
fmt.Printf("Page %d\n", i)
for _, i := range r.Files {
fmt.Printf("%s (ID = %s | parents = %v | trashed = %v)\n", i.Name, i.Id, i.Parents, i.Trashed)
}
} else {
fmt.Println("No files found.")
break
}
if pageToken = r.NextPageToken; pageToken == "" {
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment