如何使用ServiceAccount和Golang将文件上传到GoogleDrive,并与任何人分享。

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

How to upload files to GoogleDrive, and share with anyone using ServiceAccount and Golang

问题

我想上传文件并与任何人共享,从ServiceAccount Golang到GoogleDrive。但是我被这个错误困扰着。

  • 我的代码:
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("无法访问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)
}
英文:

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

答案1

得分: 1

当我看到你的脚本时,似乎在上传文本文件时,权限数据包含在元数据中。不幸的是,这不能使用。我认为这是与writable相关的问题的原因。在这种情况下,在文件上传后,请使用"Permissions: create"。当你修改脚本时,可以考虑以下修改:

从:

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)

到:

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()
  • 通过这个修改,上传的文本文件将被公开共享。

注意:

  • 根据你的评论“我已经将我的parent_id文件夹(我想要上传文件并与任何人共享的文件夹)与service_account电子邮件共享为编辑权限”,如果你想将文本文件上传到公开共享的文件夹“17n-EpJcGg0DmmWqSoJ75iIUdXDP7neoH”,我认为以下修改后的脚本也可以使用。因为在这种情况下,当文本文件上传到公开共享的文件夹时,上传的文本文件具有与文件夹相同的权限。

      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)
    

参考:

英文:

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:

huangapple
  • 本文由 发表于 2022年10月7日 09:00:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/73981337.html
匿名

发表评论

匿名网友

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

确定