英文:
GOLANG:exclude trash files from google drive
问题
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
type listAllFilesArgs struct {
query string
fields []googleapi.Field
sortOrder string
maxFiles int64
}
type Field string
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
func (self *Drive) listAllFiles(args listAllFilesArgs) ([]*drive.File, error) {
var files []*drive.File
var pageSize int64
if args.maxFiles > 0 && args.maxFiles < 1000 {
pageSize = args.maxFiles
} else {
pageSize = 1000
}
controlledStop := fmt.Errorf("Controlled stop")
err := self.service.Files.List().Q(args.query).Fields(args.fields...).OrderBy(args.sortOrder).PageSize(pageSize).Pages(context.TODO(), func(fl *drive.FileList) error {
files = append(files, fl.Files...)
// Stop when we have all the files we need
if args.maxFiles > 0 && len(files) >= int(args.maxFiles) {
return controlledStop
}
return nil
})
if err != nil && err != controlledStop {
return nil, err
}
if args.maxFiles > 0 {
n := min(len(files), int(args.maxFiles))
return files[:n], nil
}
return files, nil
}
func (self *Drive) newPathfinder() *remotePathfinder {
return &remotePathfinder{
service: self.service.Files,
files: make(map[string]*drive.File),
}
}
<!-- end snippet -->
我正在使用Go语言开发Google Drive API,以下是版本使用的详细信息:
Go版本:go1.8.1 windows/amd64
已导入的包:
"google.golang.org/api/drive/v3"
"google.golang.org/api/googleapi"
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
err = gDrive.List(listArgs)
if err != nil {
fmt.Errorf("Failed to list files: %s", err)
}
func (self *Drive) List(args ListFilesArgs) (err error) {
listArgs := listAllFilesArgs{
query: args.Query,
fields: []googleapi.Field{"nextPageToken", "files(id,name,md5Checksum,mimeType,size,createdTime,parents)"},
sortOrder: args.SortOrder,
maxFiles: args.MaxFiles,
}
files, err := self.listAllFiles(listArgs)
if err != nil {
return fmt.Errorf("Failed to list files: %s /n", err)
}
pathfinder := self.newPathfinder()
if args.AbsPath {
// Replace name with absolute path
for _, f := range files {
f.Name, err = pathfinder.absPath(f)
if err != nil {
return err
}
}
}
PrintFileList(PrintFileListArgs{
Out: args.Out,
Files: files,
NameWidth: int(args.NameWidth),
SkipHeader: args.SkipHeader,
SizeInBytes: args.SizeInBytes,
})
return
}
<!-- end snippet -->
Google Drive API for Golang用于列出包括回收站在内的文件。如何排除回收站文件,
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
type listAllFilesArgs struct {
query string
fields []googleapi.Field
sortOrder string
maxFiles int64
}
type Field string
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
func (self *Drive) listAllFiles(args listAllFilesArgs) ([]*drive.File, error) {
var files []*drive.File
var pageSize int64
if args.maxFiles > 0 && args.maxFiles < 1000 {
pageSize = args.maxFiles
} else {
pageSize = 1000
}
controlledStop := fmt.Errorf("Controlled stop")
err := self.service.Files.List().Q(args.query).Fields(args.fields...).OrderBy(args.sortOrder).PageSize(pageSize).Pages(context.TODO(), func(fl *drive.FileList) error {
files = append(files, fl.Files...)
// Stop when we have all the files we need
if args.maxFiles > 0 && len(files) >= int(args.maxFiles) {
return controlledStop
}
return nil
})
if err != nil && err != controlledStop {
return nil, err
}
if args.maxFiles > 0 {
n := min(len(files), int(args.maxFiles))
return files[:n], nil
}
return files, nil
}
func (self *Drive) newPathfinder() *remotePathfinder {
return &remotePathfinder{
service: self.service.Files,
files: make(map[string]*drive.File),
}
}
<!-- end snippet -->
i am using go for my googledrive api development following wre the details of version usage,
go version go1.8.1 windows/amd64
and imported
"google.golang.org/api/drive/v3"
"google.golang.org/api/googleapi"
for development
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
err = gDrive.List(listArgs)
if err != nil {
fmt.Errorf("Failed to list files: %s", err)
}
func (self *Drive) List(args ListFilesArgs) (err error) {
listArgs := listAllFilesArgs{
query: args.Query,
fields: []googleapi.Field{"nextPageToken", "files(id,name,md5Checksum,mimeType,size,createdTime,parents)"},
sortOrder: args.SortOrder,
maxFiles: args.MaxFiles,
}
files, err := self.listAllFiles(listArgs)
if err != nil {
return fmt.Errorf("Failed to list files: %s /n", err)
}
pathfinder := self.newPathfinder()
if args.AbsPath {
// Replace name with absolute path
for _, f := range files {
f.Name, err = pathfinder.absPath(f)
if err != nil {
return err
}
}
}
PrintFileList(PrintFileListArgs{
Out: args.Out,
Files: files,
NameWidth: int(args.NameWidth),
SkipHeader: args.SkipHeader,
SizeInBytes: args.SizeInBytes,
})
return
}
<!-- end snippet -->
google drive api for Golang listing files including trash.
How to exclude trash files,
答案1
得分: 1
要筛选从Google Drive检索的文件,您需要根据"搜索文件"中描述的方式指定搜索参数。将其作为Q(...)
的参数传递,例如
假设您想要搜索名称包含
myfile
且未在回收站中的
那么代码将如下所示:
err := self.service.Files.List().
Q("mimeType='application/pdf' and name contains 'myfile' and trashed=false")
英文:
To filter files that you want to retrieve from google drive, you need to specify search parameters as described in "Search for Files". Pass it as the argument of Q(...)
, for example
> Suppose you want to search for pdf
file with a name containing myfile
and not in trashed
.
then the code will look like
err := self.service.Files.List().
Q("mimeType='application/pdf' and name contains 'myfile' and trashed=false")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论