google-drive-client

module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2023 License: MIT

README

google-drive-client

A simple Go client for interacting with Google Drive. This client provides an easy way to create folders and upload files to Google Drive using the Google Drive API.

More functionalities will be added.

setup

Before using this client, you need to set up a Google Cloud Project and enable the Google Drive API. Follow these steps:

  1. Create a new project in the Google Cloud Console.
  2. Enable the Google Drive API for your project.
  3. Create credentials (service account key) for your project:
    • Go to the "Credentials" page.
    • Click "Create Credentials" and select "Service account key".
    • Choose or create a new service account.
    • Set the role to Editor or another role that grants the necessary permissions.
    • Select JSON for the key type and download the file.
  4. Save the downloaded JSON file in the creds directory.

For more detailed instructions, see the Google Drive API documentation.

example

Here's a basic example of how to use this client:

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/tiagomelo/google-drive-client/googledrive"
)

func main() {
    ctx := context.Background()
    client, err := googledrive.New(ctx, credsFilePath)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    const parentFolderId = `1LTtYxqYUMoA1IzHmlx_RMVdkWgtskMxO`
    folder, err := client.CreateFolder("test folder", parentFolderId)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Printf("folder.Id: %v\n", folder)

    file, err := os.Open("test.txt")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    defer file.Close()

    uploadedFile, err := client.UploadFile(file, folder)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Printf("uploadedFile: %v\n", uploadedFile)
}

unit tests

make test

Directories

Path Synopsis
Package googledrive provides an abstraction layer over the Google Drive API.
Package googledrive provides an abstraction layer over the Google Drive API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL