dousuochu7291 2017-05-10 07:43
浏览 51
已采纳

GOLANG:从Google云端硬盘中排除垃圾文件

type listAllFilesArgs struct {
    query     string
    fields    []googleapi.Field
    sortOrder string
    maxFiles  int64
}
type Field string

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),
    }
}

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

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
}

google drive api for Golang listing files including trash. How to exclude trash files,

</div>
  • 写回答

1条回答 默认 最新

  • duanque3125 2017-05-10 23:57
    关注

    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")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Mybatis#的问题:怎么办重复数据如下"KSDM":37,"KSMC":"呼吸内科门诊","ksdm":37,"ksmc":"呼吸内科门诊"xml文件填写
  • ¥40 servlet的web程序部署出错
  • ¥50 activiti 新建流程系列问题
  • ¥50 为什么我版本升级之后运行速度变慢5倍??
  • ¥15 如何在gazebo中加载机械臂和机械手
  • ¥15 纯运放实现隔离采样方案设计
  • ¥20 easyconnect无法连接后缀带.com的网站,一直显示找不到服务器ip地址,但是带有数字的网站又是可以连接上的,如何解决
  • ¥15 电脑开机过商标后就直接这样,求解各位
  • ¥15 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!