Home > Software design >  How to upload files to GoogleDrive, and share with anyone using ServiceAccount and Golang
How to upload files to GoogleDrive, and share with anyone using ServiceAccount and Golang

Time:10-07

I want to upload the file and share it with anyone from ServiceAccount Golang to GoogleDrive. But I am stuck in with this error.

  • My Code:
package main

import (
    "fmt"
    "golang.org/x/net/context"
    "google.golang.org/api/drive/v3"
    "google.golang.org/api/googleapi"
    "google.golang.org/api/option"
    "log"
    "os"
)

func main() {
    srv, err := drive.NewService(context.Background(), option.WithCredentialsFile("key.json"))
    if err != nil {
        log.Fatal("Unable to access Drive API:", err)
    }

    filename := "./lemon.txt"
    file, err := os.Open(filename)
    if err != nil {
        log.Fatalln(err)
    }

    stat, err := file.Stat()
    if err != nil {
        log.Fatalln(err)
    }
    defer file.Close()

    res, err := srv.Files.Create(
        &drive.File{
            Parents: []string{"17n-EpJcGg0DmmWqSoJ75iIUdXDP7neoH"},
            Name:    "banana.txt",
            Permissions: []*drive.Permission{
                {
                    Role: "reader",
                    Type: "anyone",
                },
            },
        },
    ).Media(file, googleapi.ChunkSize(int(stat.Size()))).Do()
    if err != nil {
        log.Fatalln(err)
    }
    
    fmt.Printf("%s\n", res.Id)
}
  • Error: [Get 403][1] [1]: https://i.stack.imgur.com/mzwxv.png

CodePudding user response:

When I saw your script, it seems that when a text file is uploaded, the permission data is included in the metadata. Unfortunately, this cannot be used. I think that this is the reason for your issue related to writable. In this case, after the file was uploaded, please use "Permissions: create". When your script is modified, how about the following modification?

From:

res, err := srv.Files.Create(
    &drive.File{
        Parents: []string{"17n-EpJcGg0DmmWqSoJ75iIUdXDP7neoH"},
        Name:    "banana.txt",
        Permissions: []*drive.Permission{
            {
                Role: "reader",
                Type: "anyone",
            },
        },
    },
).Media(file, googleapi.ChunkSize(int(stat.Size()))).Do()
if err != nil {
    log.Fatalln(err)
}

fmt.Printf("%s\n", res.Id)

To:

res, err := srv.Files.Create(
    &drive.File{
        Parents: []string{"17n-EpJcGg0DmmWqSoJ75iIUdXDP7neoH"},
        Name:    "banana.txt",
    },
).Media(file, googleapi.ChunkSize(int(stat.Size()))).Do()
if err != nil {
    log.Fatalln(err)
}
fmt.Printf("%s\n", res.Id)

res2, err := srv.Permissions.Create(res.Id, &drive.Permission{
    Role: "reader",
    Type: "anyone",
}).Do()
  • By this modification, the uploaded text file is publicly shared.

Note:

  • From your comment of I already share my parent_id folder (the folder I want to upload files and share with anyone) with the service_account email as editor permission., if you want to upload a text file to the publicly shared folder "17n-EpJcGg0DmmWqSoJ75iIUdXDP7neoH", I think that the following modified script can be also used. Because, in this case, when a text file is uploaded to the publicly shared folder, the uploaded text file has the same permissions as the folder.

      res, err := srv.Files.Create(
          &drive.File{
              Parents: []string{"17n-EpJcGg0DmmWqSoJ75iIUdXDP7neoH"},
              Name:    "banana.txt",
          },
      ).Media(file, googleapi.ChunkSize(int(stat.Size()))).Do()
      if err != nil {
          log.Fatalln(err)
      }
      fmt.Printf("%s\n", res.Id)
    

Reference:

  • Related