gdrivegokit

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 12 Imported by: 0

README

GDriveGoKit

GDriveGoKit is a lightweight, SDK-style Go library designed to simplify integrations with the Google Drive API v3. It handles the complexities of OAuth2 flows, recursive folder management, and concurrent uploads so you can focus on your application logic.

Go Reference


Features

  • OAuth2 Ready: Built-in token caching and automated authorization flows.
  • Smart Folders: Automatic recursive folder creation (e.g., a/b/c). Reuses existing folders to prevent duplicates.
  • MIME Detection: Automatic content-type detection if not explicitly provided.
  • High Performance: Concurrent batch uploads with per-file error tracking.
  • Context-Aware: Fully supports context.Context for timeouts and cancellations.

Installation

go get [github.com/4CHILL3S101/gdrivegokit](https://github.com/4CHILL3S101/gdrivegokit)

Prerequisites

-Google Cloud Setup:

Create a project in the Google Cloud Console.

Enable the Google Drive API.

Create OAuth Client ID credentials (select "Desktop App").

Download the JSON file and rename it to credentials.json.

Authentication Flow

First Run: The library generates an authorization URL in the terminal.

Consent: Follow the URL, log in, and paste the resulting code back into the terminal.

Persistence: The token is saved locally (e.g., token.json).

Future Runs: Authentication happens automatically using the cached token.


Quick Start

  • Single File Upload
package main

import (
	"context"
	"fmt"
	"os"

	"[github.com/4CHILL3S101/gdrivegokit](https://github.com/4CHILL3S101/gdrivegokit)"
)

func main() {
	ctx := context.Background()

	// 1. Load Google OAuth credentials
	creds, err := os.ReadFile("credentials.json")
	if err != nil {
		panic(err)
	}

	// 2. Setup token provider (handles caching)
	tokenProvider := &gdrivegokit.FileTokenProvider{
		Path: "token.json",
	}

	// 3. Initialize Uploader
	uploader, err := gdrivegokit.NewUploader(ctx, creds, tokenProvider)
	if err != nil {
		panic(err)
	}

	// 4. Upload (Folders are created automatically)
	fileID, err := uploader.UploadFile(
		"example.png",
		"users/123/images",
		"image/png", // Leave empty for auto-detection
	)

	if err != nil {
		fmt.Printf("Upload failed: %v\n", err)
		return
	}

	fmt.Printf("Success! File ID: %s\n", fileID)
}



Concurrent Batch Upload


files := []string{"doc1.pdf", "doc2.pdf", "image1.png"}

// Uploads all files concurrently to the specified path
results := uploader.UploadFilesBatch(
	files,
	"backups/october",
	"", // Auto-detect MIME type
)

for fileName, res := range results {
	if res.Err != nil {
		fmt.Printf(" %s failed: %v\n", fileName, res.Err)
	} else {
		fmt.Printf(" %s uploaded -> ID: %s\n", fileName, res.FileID)
	}
}



Automatic Folder Management

  • You can use nested paths directly without pre-creating them in Drive:
	users/123/images

	backups/2026/january

How it works:

The library splits the path.

It checks if users exists, then 123, then images.

It creates only the missing segments and returns the final folder ID.


API Reference

NewUploader func NewUploader(ctx context.Context, creds []byte, tp TokenProvider) (*Uploader, error)

Initializes the Google Drive client. Requires OAuth2 credentials and a token storage strategy.

#UploadFile func (u *Uploader) UploadFile(filePath, folderPath, mimeType string) (string, error)

Uploads a single file. Returns the Google Drive File ID.

#UploadFilesBatch func (u *Uploader) UploadFilesBatch(paths []string, folderPath, mimeType string) map[string]UploadResult

Performs concurrent uploads. Returns a map where the key is the file path and the value is an UploadResult containing the FileID or Err.


License

Distributed under the MIT License. See LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDriveService

func GetDriveService(
	ctx context.Context,
	credentialsJSON []byte,
	tokenProvider TokenProvider,
) (*drive.Service, error)

GetDriveService initializes a Google Drive client using OAuth2 config and token provider.

func GetOrCreateFolder

func GetOrCreateFolder(srv *drive.Service, parentID, folderName string) (string, error)

GetOrCreateFolder ensures a folder exists under parentID and returns its ID. Returns error instead of exiting.

func GetOrCreateNestedFolder

func GetOrCreateNestedFolder(srv *drive.Service, path string) (string, error)

GetOrCreateNestedFolder ensures a nested path of folders exists and returns the final folder ID.

Types

type FileTokenProvider

type FileTokenProvider struct {
	Path string
}

func (*FileTokenProvider) Token

func (p *FileTokenProvider) Token(ctx context.Context, config *oauth2.Config) (*oauth2.Token, error)

type TokenProvider

type TokenProvider interface {
	Token(ctx context.Context, config *oauth2.Config) (*oauth2.Token, error)
}

TokenProvider allows flexible auth (file, memory, DB, etc.)

type UploadResult

type UploadResult struct {
	FileID string
	Err    error
}

UploadResult holds the result of a file upload

type Uploader

type Uploader struct {
	Service *drive.Service
}

Uploader is the main struct for uploading files to Drive

func NewUploader

func NewUploader(
	ctx context.Context,
	credentialsJSON []byte,
	tokenProvider TokenProvider,
) (*Uploader, error)

func (*Uploader) UploadFile

func (u *Uploader) UploadFile(filePath, folderPath, mimeType string) (string, error)

UploadFile uploads a single file to the specified folder path

func (*Uploader) UploadFilesBatch

func (u *Uploader) UploadFilesBatch(
	filePaths []string,
	folderPath, mimeType string,
) map[string]UploadResult

UploadFilesBatch uploads multiple files concurrently. Returns a map[filePath]UploadResult with FileID and error for each file.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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