排除垃圾文件从谷歌云端硬盘中。

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

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 &gt; 0 &amp;&amp; args.maxFiles &lt; 1000 {
		pageSize = args.maxFiles
	} else {
		pageSize = 1000
	}
	controlledStop := fmt.Errorf(&quot;Controlled stop&quot;)
	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 &gt; 0 &amp;&amp; len(files) &gt;= int(args.maxFiles) {
			return controlledStop
		}

		return nil
	})

	if err != nil &amp;&amp; err != controlledStop {
		return nil, err
	}

	if args.maxFiles &gt; 0 {
		n := min(len(files), int(args.maxFiles))
		return files[:n], nil
	}

	return files, nil
}

func (self *Drive) newPathfinder() *remotePathfinder {
	return &amp;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(&quot;Failed to list files: %s&quot;, err)
	}

func (self *Drive) List(args ListFilesArgs) (err error) {
	listArgs := listAllFilesArgs{
		query:     args.Query,
		fields:    []googleapi.Field{&quot;nextPageToken&quot;, &quot;files(id,name,md5Checksum,mimeType,size,createdTime,parents)&quot;},
		sortOrder: args.SortOrder,
		maxFiles:  args.MaxFiles,
	}
	files, err := self.listAllFiles(listArgs)
	if err != nil {
		return fmt.Errorf(&quot;Failed to list files: %s /n&quot;, 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 &gt; 0 &amp;&amp; args.maxFiles &lt; 1000 {
		pageSize = args.maxFiles
	} else {
		pageSize = 1000
	}
	controlledStop := fmt.Errorf(&quot;Controlled stop&quot;)
	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 &gt; 0 &amp;&amp; len(files) &gt;= int(args.maxFiles) {
			return controlledStop
		}

		return nil
	})

	if err != nil &amp;&amp; err != controlledStop {
		return nil, err
	}

	if args.maxFiles &gt; 0 {
		n := min(len(files), int(args.maxFiles))
		return files[:n], nil
	}

	return files, nil
}

func (self *Drive) newPathfinder() *remotePathfinder {
	return &amp;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(&quot;Failed to list files: %s&quot;, err)
	}

func (self *Drive) List(args ListFilesArgs) (err error) {
	listArgs := listAllFilesArgs{
		query:     args.Query,
		fields:    []googleapi.Field{&quot;nextPageToken&quot;, &quot;files(id,name,md5Checksum,mimeType,size,createdTime,parents)&quot;},
		sortOrder: args.SortOrder,
		maxFiles:  args.MaxFiles,
	}
	files, err := self.listAllFiles(listArgs)
	if err != nil {
		return fmt.Errorf(&quot;Failed to list files: %s /n&quot;, 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且未在回收站中的pdf文件。

那么代码将如下所示:

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(&quot;mimeType=&#39;application/pdf&#39; and name contains &#39;myfile&#39; and trashed=false&quot;)

huangapple
  • 本文由 发表于 2017年5月10日 15:43:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/43886361.html
匿名

发表评论

匿名网友

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

确定