Here is a program that can delete a given file. You’ll need to compile it first and then provide a token file as well as the file to delete:
go build gcd_delete.go
./gcd_delete <token file> <file to delete>
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/drive/v3"
"google.golang.org/api/option"
)
type GCDConfig struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Endpoint oauth2.Endpoint `json:"end_point"`
Token oauth2.Token `json:"token"`
}
func getInfo(service *drive.Service, parent string, name string) (exist bool, fileID string) {
query := "name = '" + name + "' and '" + parent + "' in parents and trashed != true"
if parent == "" {
query = "name = '" + name + "' and ('root' in parents or sharedWithMe) and trashed != true"
}
fileList, err := service.Files.List().Q(query).Fields("files(name, mimeType, id, size)").Do()
if err != nil {
fmt.Printf("Fail to check %s: %v\n", name, err)
return false, ""
}
if len(fileList.Files) == 0 {
return false, ""
}
if len(fileList.Files) > 1 {
fmt.Printf("More than one file named '%s' exists\n", name)
for _, file := range fileList.Files {
fmt.Printf("%s %s %s %d\n", file.Name, file.MimeType, file.Id, file.Size)
}
}
return true, fileList.Files[0].Id
}
func main() {
ctx := context.Background()
description, err := ioutil.ReadFile(os.Args[1])
if err != nil {
log.Fatalf("Unable to read the token file: %v", err)
return
}
var object map[string]interface{}
err = json.Unmarshal(description, &object)
if err != nil {
log.Fatalf("Failed to parse the token file: %v", err)
return
}
isServiceAccount := false
if value, ok := object["type"]; ok {
if authType, ok := value.(string); ok && authType == "service_account" {
isServiceAccount = true
}
}
var tokenSource oauth2.TokenSource
scope := drive.DriveScope
if isServiceAccount {
if newScope, ok := object["scope"]; ok {
scope = newScope.(string)
}
config, err := google.JWTConfigFromJSON(description, scope)
if err != nil {
log.Fatalf("Failed to create the oauth config: %v", err)
return
}
if subject, ok := object["subject"]; ok {
config.Subject = subject.(string)
}
tokenSource = config.TokenSource(ctx)
} else {
gcdConfig := &GCDConfig{}
if err := json.Unmarshal(description, gcdConfig); err != nil {
log.Fatalf("Failed to create the oauth config: %v", err)
return
}
config := oauth2.Config{
ClientID: gcdConfig.ClientID,
ClientSecret: gcdConfig.ClientSecret,
Endpoint: gcdConfig.Endpoint,
}
tokenSource = config.TokenSource(ctx, &gcdConfig.Token)
}
service, err := drive.NewService(ctx, option.WithTokenSource(tokenSource))
if err != nil {
log.Fatalf("Unable to create drive Client %v", err)
return
}
filePath := os.Args[2]
names := strings.Split(filePath, "/")
parentID := ""
for _, name := range names {
exist, fileID := getInfo(service, parentID, name)
if !exist {
log.Fatalf("%s doesn't exist", filePath)
return
}
parentID = fileID
}
log.Printf("%s: %s", filePath, parentID)
err = service.Files.Delete(parentID).SupportsAllDrives(true).Fields("id").Do()
if err != nil {
log.Fatalf("Failed to delete %s: %v", filePath, err)
} else {
log.Printf("%s has been successfully deleted", filePath)
}
}