Reputation: 1447
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?
Upvotes: 2
Views: 525
Reputation: 117301
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}
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
}
Upvotes: 1