英文:
File uploads in Version 3 of the Google Drive SDK
问题
我有一些基于以下Golang示例的代码,示例链接为https://developers.google.com/drive/v2/reference/files/insert。
// InsertFile从给定的文件和详细信息中创建一个新文件到Drive中
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: 在结构体字面值中未知的drive.File字段'Title'
./main.go:127: 未定义: drive.ParentReference
./main.go:128: 未定义: drive.ParentReference
./main.go:131: service.Files.Insert未定义(*drive.FilesService类型没有Insert字段或方法)
我知道第一个错误中的Title应该改为Name,但我不确定drive.ParentReference被什么替代,以及在SDK的版本3中service.Files.Insert被替换为什么,我也没有找到与上述链接等效的V3文档。
英文:
I have some code that is based on the following Golang example at https://developers.google.com/drive/v2/reference/files/insert.
// 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
}
When I switch to version 3, the errors below are thrown.
./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)
I know that Title should be changed to Name in the first error but I'm not sure what replaced drive.ParentReference or service.Files.Insert in Version 3 of the SDK and I haven't been able to find anything equivalent to the link above in the V3 docs.
答案1
得分: 2
这是一个值得阅读的Google API Go源代码的链接:这里。你可以看到ParentReference
在v2 API代码中存在(链接),但在v3中不存在(链接)。从v2到v3,API似乎发生了重大变化。
根据这些变化,以下是上传文件的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()
}
请注意,这只是一个草图,具体实现可能需要根据你的需求进行调整。
英文:
It's worth perusing the Google API go source code here. You can see how ParentReference
exists in the v2 API code but does not exist in v3. The API appears to have changed significantly from v2 to v3.
Extrapolating from these changes, here's a sketch of what the v3 equivalent for uploading a file might look like:
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()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论