gpt4 book ai didi

go - Google Drive SDK 版本 3 中的文件上传

转载 作者:数据小太阳 更新时间:2023-10-29 03:06:33 25 4
gpt4 key购买 nike

我在 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 文档中上面的链接。

最佳答案

Google API go 源代码值得一读here .你可以看到如何ParentReference exists in the v2 API code但是does 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()
}

关于go - Google Drive SDK 版本 3 中的文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34749158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com