Google云端硬盘SDK第3版中的文件上传

时间:2016-01-12 16:42:35

标签: go google-drive-api

我有一些代码基于https://developers.google.com/drive/v2/reference/files/insert的以下Golang示例。

// InsertFile creates a new file in Drive from the given file and details
func InsertFile(d *drive.Service, title string, description string,
parentId string, mimeType string, filename string) (*drive.File, error) {
             .
             .
             . 
  f := &drive.File{Title: title, Description: description, MimeType: mimeType}
  if parentId != "" {
    p := &drive.ParentReference{Id: parentId}
    f.Parents = []*drive.ParentReference{p}
  }
  r, err := d.Files.Insert(f).Media(m).Do()
  if err != nil {
    fmt.Printf("An error occurred: %v\n", err)
    return nil, err
  }
  return r, nil
}

当我切换到版本3时,会抛出以下错误。

./main.go:125: unknown drive.File field 'Title' in struct literal
./main.go:127: undefined: drive.ParentReference
./main.go:128: undefined: drive.ParentReference
./main.go:131: service.Files.Insert undefined (type *drive.FilesService has no field or method Insert)

我知道在第一个错误中应该将Title更改为Name但是我不确定是什么替换了SDK的版本3中的drive.ParentReference或service.Files.Insert,我无法找到任何等效的东西到V3 docs上面的链接。

1 个答案:

答案 0 :(得分:2)

值得仔细阅读Google API的源代码here。您可以看到ParentReference exists in the v2 API codedoes not exist in v3的方式。 API似乎已从v2发生显着变化到v3。

从这些变化中推断,下面是上传文件的v3等效内容的草图:

import "google.golang.org/api/drive/v3"
func InsertFile(d *drive.Service, title string, description string, mimeType string, filename string) (*drive.File, error) {
    f := &drive.File{Name: filename, Description: description, MimeType: mimeType}
    return d.Files.Create(f).do()
}