https://github.com/bounoable/godrive
Multi-provider cloud storage drive.
https://github.com/bounoable/godrive
go go-library golang golang-library storage
Last synced: 3 months ago
JSON representation
Multi-provider cloud storage drive.
- Host: GitHub
- URL: https://github.com/bounoable/godrive
- Owner: bounoable
- License: mit
- Created: 2020-02-17T00:23:26.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-08-25T15:11:27.000Z (almost 2 years ago)
- Last Synced: 2025-01-18T06:08:18.274Z (5 months ago)
- Topics: go, go-library, golang, golang-library, storage
- Language: Go
- Homepage:
- Size: 127 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
godrive - Cloud Storage Library
This library provides a uniform access to multiple storage providers and a central configuration for all storage disks.
It autowires the storage disks from a single YAML configuration file.## Install
```sh
go get github.com/bounoable/godrive
```## Usage
[**Read the GoDocs (pkg.go.dev)**](https://pkg.go.dev/github.com/bounoable/godrive)
### Autowire from YAML configuration
1. Create configuration
```yaml
default: main # Default disk to usedisks:
main: # Specify a disk name
provider: s3 # The storage provider
config: # Configuration for the storage provider
region: us-east-2
bucket: images
accessKeyId: ${AWS_ACCESS_KEY_ID} # Use environment variable
secretAccessKey: ${AWS_SECRET_ACCESS_KEY}
public: true
videos:
provider: gcs
config:
serviceAccount: /path/to/service/account.json
bucket: uploads
public: true
```2. Create manager
```go
package mainimport (
"github.com/bounoable/godrive"
"github.com/bounoable/godrive/s3"
"github.com/bounoable/godrive/gcs"
)func main() {
// Initialize autowire & register providers
aw := godrive.NewAutoWire(
s3.Register,
gcs.Register,
)// Load disk configuration
err := aw.Load("/path/to/config.yml")
if err != nil {
panic(err)
}// Create disk manager
manager, err := aw.NewManager(context.Background())
if err != nil {
panic(err)
}// Get disk by name and use it
disk, _ := manager.Disk("videos")
err = disk.Put(context.Background(), "path/on/disk.txt", []byte("Hi."))
content, err := disk.Get(context.Background(), "path/on/disk.txt")
err = disk.Delete(context.Background(), "path/on/disk.txt")// or use the default disk
err = manager.Put(context.Background(), "path/on/disk.txt", []byte("Hi."))
content, err := manager.Get(context.Background(), "path/on/disk.txt")
err = manager.Delete(context.Background(), "path/on/disk.txt")
}
```### Use without autowire
```go
package mainimport (
"cloud.google.com/go/storage"
"github.com/aws/aws-sdk-go-v2/aws"
awss3 "github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/bounoable/godrive"
"github.com/bounoable/godrive/s3"
"github.com/bounoable/godrive/gcs"
"google.golang.org/api/option"
)func main() {
// Create manager and add disks manually
manager := godrive.New()s3client := awss3.New(aws.Config{})
manager.Configure("main", s3.NewDisk(s3client, "REGION", "BUCKET", s3.Public(true)))gcsclient, err := storage.NewClient(context.Backgroud())
manager.Configure("videos", gcs.NewDisk(gcsclient, "BUCKET", gcs.Public(true)))// Get disk by name and use it
disk, _ := manager.Disk("videos")
err = disk.Put(context.Background(), "path/on/disk.txt", []byte("Hi."))
content, err := disk.Get(context.Background(), "path/on/disk.txt")
err = disk.Delete(context.Background(), "path/on/disk.txt")// or use the default disk
err = manager.Put(context.Background(), "path/on/disk.txt", []byte("Hi."))
content, err := manager.Get(context.Background(), "path/on/disk.txt")
err = manager.Delete(context.Background(), "path/on/disk.txt")
}
```