无法将文件上传到Google Drive

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

Unable to upload files to Google Drive

问题

我按照这个Github gist上的说明将文件上传到Google Drive。我在我的Drive上创建了一个文件夹,并与我的服务帐户(电子邮件)共享,但我找不到文件夹中的文件。当我尝试列出文件时,结果如下所示:

file.txt (1HeKXuIxR-ZfPZN5cAGZN9o5eVUOJErdo) 
file.txt (16O-nOKhfwvcQTErnPnBp-64cpLCIBT_T) 
file.txt (1f99E4wNPOgHxzcNDkqQn0UG6VcxBpwC1) 
file.txt (1UywaSPuV0yb09QgnN6DDz679x9COYbAR) 
file.txt (1-WUhIniHrC9ANiTA7mWBE4-MyW-57vrY) 
file.txt (1nJ1FCSqdyNQWrIcbSXtkwv8GJGCQT6Xl) 
file.txt (1tRMbI5WyPc5QeawE6OGObe_b798aPVbV) 
file.txt (1yCh0Tn71jdN_EBunGRZ3HnL285zdWaL5) 
file.txt (1yKnV19SCET94QteaOXwknVp-9VtEZnxO) 
file.txt (1LYh47IoF_gC98mZ68wyOU2K9e8F7qemZ) 
file.txt (1P4fJzOCdVD7lCujNvJe9yhfL-B8pMk9S) 
file.txt (1cv7xiWxpwlSt6_FMIJigHR2OoPMoevRq) 
file.txt (1fWhsrLXAC5XbUj351ENY46d7tgb7HpZT) 
file.txt (18wFSwiZEUJHjQVrccsyQcgL24NF5lo5q) 
sharedFolder (1cteTTwEmv4fV9p6L_gpIEaCIYCRhdGYo) 
file.txt (1TLNaK0K4zDy9nfE3mN1sYtlySn_tzJxR) 

sharedFolder是我创建的要与服务帐户共享的文件夹。file.txt是我正在尝试上传的文件。

我在这里漏掉了什么?

英文:

I followed the instructions on this Github gist to upload files to Google Drive. I created a folder on my drive and shared with my service account (email) but I can't find the files on the folder. When I try to list the files, I get them as shown below:

file.txt (1HeKXuIxR-ZfPZN5cAGZN9o5eVUOJErdo) 
file.txt (16O-nOKhfwvcQTErnPnBp-64cpLCIBT_T) 
file.txt (1f99E4wNPOgHxzcNDkqQn0UG6VcxBpwC1) 
file.txt (1UywaSPuV0yb09QgnN6DDz679x9COYbAR) 
file.txt (1-WUhIniHrC9ANiTA7mWBE4-MyW-57vrY) 
file.txt (1nJ1FCSqdyNQWrIcbSXtkwv8GJGCQT6Xl) 
file.txt (1tRMbI5WyPc5QeawE6OGObe_b798aPVbV) 
file.txt (1yCh0Tn71jdN_EBunGRZ3HnL285zdWaL5) 
file.txt (1yKnV19SCET94QteaOXwknVp-9VtEZnxO) 
file.txt (1LYh47IoF_gC98mZ68wyOU2K9e8F7qemZ) 
file.txt (1P4fJzOCdVD7lCujNvJe9yhfL-B8pMk9S) 
file.txt (1cv7xiWxpwlSt6_FMIJigHR2OoPMoevRq) 
file.txt (1fWhsrLXAC5XbUj351ENY46d7tgb7HpZT) 
file.txt (18wFSwiZEUJHjQVrccsyQcgL24NF5lo5q) 
sharedFolder (1cteTTwEmv4fV9p6L_gpIEaCIYCRhdGYo) 
file.txt (1TLNaK0K4zDy9nfE3mN1sYtlySn_tzJxR) 

sharedFolder is the folder I created to be shared with the service account. file.txt is the file I am trying to upload.

What am I missing here?

答案1

得分: 1

首先,我想说我不是GO开发者,我可以阅读你的代码,但不太了解其他方面。

你需要明白创建文件有两个步骤。

首先,你需要将文件的元数据设置为请求的正文部分,然后上传文件流。

在这里,你似乎设置了文件名的元数据:

f := &drive.File{Name: filename}

但你只设置了文件名,这意味着文件可能被上传到服务账户的根目录。执行文件列表操作,你应该能在那里看到你的文件。

要设置文件上传到的目录,你需要在元数据中设置父目录的值。猜测的代码可能是这样的:

f := &drive.File{Name: filename, Parents: directoryId}

代码

经过一番搜索,我找到了下面的代码,它似乎设置了父目录的ID:

// 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
}
英文:

Okay to begin with i want to say i am not a GO developer i can read your code but not much more.

The thing you need to understand is that creating a file is done in two parts.

first you set the MetaData for the file as the post body of the request then you upload the filestrea.

you apear to be setting the file name metadata here

f := &drive.File{Name: filename}

but you are only setting the file name, which means that the file is probably being uploaded to the service accounts root directory. do a file.list and you should see your files there.

To set the directory you want the file uploaded to you need to set the parents value the meta data. wild guess would be something like this.

f := &drive.File{Name: filename, Parents: directoryId}

code

after a bit of googleing i found this which appears to set the parentId

// 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
}

huangapple
  • 本文由 发表于 2022年8月18日 17:45:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/73400848.html
匿名

发表评论

匿名网友

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

确定