gs

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2025 License: MIT Imports: 14 Imported by: 0

README

storage

This implementation provides you a set of standard functions to consume be the end user without worrying about any underlyig complexities.



Installation

go get oss.nandlabs.io/golly-gcp/storge

Features

A number of features are provided out of the box.

Storage File features such as

  • Read a File
  • Write content to a file
  • List all the files of a bucket/folder
  • Get information about a file
  • Add metadata to a file
  • Read metadat value of a file
  • Delete a file

Storage File System features such as

  • Create a file or a folder
  • Open a file in a given location

Usage

Setup the storage library in order to start using it. Under you main pacakge, you can add an init function or any method of your choice to initiate the library

The Priority of the Registered Provider is as follows

URL > HOST > Scheme("gs") > default
package main

import (
    "context"

    "oss.nandlabs.io/golly-gcp/gcpsvc"
)

func init() {
    credentialsPath := "{enter the path of the credentials}"
    config := gcpsvc.Config{
        ProjectId: "project-id",
    }
    config.SetCredentialFile(credentialsPath)
    gcpsvc.Manager.Register("gs", config)
}

URL Format to use

gs://bucketName/folderName.../fileName

Examples

  1. Create a file

    package main
    
    import (
        _ "oss.nandlabs.io/golly-gcp/storage"
        "oss.nandlabs.io/golly/vfs"
    )
    
    func main() {
        manager := vfs.GetManager()
        u, err := url.Parse("gs://bucketName")
        if err != nil {
            // handle error
        }
        file, err := manager.Create(u)
    
        if err != nil {
            // handle error
        }
        fmt.Println(file.Info())
    }
    
  2. Create a folder

    package main
    
    import (
        _ "oss.nandlabs.io/golly-gcp/storage"
        "oss.nandlabs.io/golly/vfs"
    )
    
    func main() {
        manager := vfs.GetManager()
        fmt.Printf("%v\n", manager)
        #
        u, err := url.Parse("gs://{bucket_name}/folder_1/")
        fmt.Println(u)
        if err != nil {
            // handle error
            fmt.Println(err)
            return
        }
        resp, err := manager.Create(u)
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Println(resp)
    }
    
  3. Read a file

    package main
    
    import (
        _ "oss.nandlabs.io/golly-gcp/storage"
        "oss.nandlabs.io/golly/vfs"
    )
    
    func main() {
        manager := vfs.GetManager()
        u, err := url.Parse("gs://{bucket_name}/folder_1/gopher-image.png")
        if err != nil {
            fmt.Println(err)
            return
        }
        file, err := manager.Open(u)
        if err != nil {
            fmt.Println(err)
            return
        }
        buffer := make([]byte, 1024)
        n, err := file.Read(buffer)
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Println(n)
    }
    
  4. Delete a file

    package main
    
    import (
        _ "oss.nandlabs.io/golly-gcp/storage"
        "oss.nandlabs.io/golly/vfs"
    )
    
    func main() {
        manager := vfs.GetManager()
        // folder1 - was a file
        u, err := url.Parse("gs://golly-test-app/folder1")
        if err != nil {
            fmt.Println(err)
            return
        }
        file, err := manager.Open(u)
        if err != nil {
            fmt.Println(err)
            return
        }
        err = file.Delete()
        if err != nil {
            fmt.Println(err)
            return
        }
    }
    
  5. Write a file

    package main
    
    import (
        _ "oss.nandlabs.io/golly-gcp/storage"
        "oss.nandlabs.io/golly/vfs"
    )
    
    func main() {
    
    }
    
  6. List all the files in a bucket

    package main
    
    import (
        _ "oss.nandlabs.io/golly-gcp/storage"
        "oss.nandlabs.io/golly/vfs"
    )
    
    func main() {
        manager := vfs.GetManager()
        u, err := url.Parse("gs://{bucket_name}")
        if err != nil {
            fmt.Println(err)
            return
        }
        file, err := manager.Open(u)
        if err != nil {
            fmt.Println(err)
            return
        }
        files, err := file.ListAll()
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Print(files)
    }
    
  7. Get File Info of an object

    package main
    
    import (
        _ "oss.nandlabs.io/golly-gcp/storage"
        "oss.nandlabs.io/golly/vfs"
    )
    
    func main() {
        manager := vfs.GetManager()
        u, err := url.Parse("gs://{bucket_name}/folder_1/gopher-image.png")
        if err != nil {
            fmt.Println(err)
            return
        }
        file, err := manager.Open(u)
        if err != nil {
            fmt.Println(err)
            return
        }
        info, err := file.Info()
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Println(info)
    }
    
  8. Get metadata of an object

    package main
    
    import (
        _ "oss.nandlabs.io/golly-gcp/storage"
        "oss.nandlabs.io/golly/vfs"
    )
    
    func main() {
        manager := vfs.GetManager()
        u, err := url.Parse("gs://{bucket_name}/folder_1/gopher-image.png")
        if err != nil {
            fmt.Println(err)
            return
        }
        file, err := manager.Open(u)
        if err != nil {
            fmt.Println(err)
            return
        }
        val, err := file.GetProperty("unique-code")
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Printf("property value:: %v\n", val)
    }
    
  9. Add metadata to an object

    package main
    
    import (
        _ "oss.nandlabs.io/golly-gcp/storage"
        "oss.nandlabs.io/golly/vfs"
    )
    
    func main() {
        manager := vfs.GetManager()
        u, err := url.Parse("gs://{bucket_name}/folder_1/gopher-image.png")
        if err != nil {
            fmt.Println(err)
            return
        }
        file, err := manager.Open(u)
        if err != nil {
            fmt.Println(err)
            return
        }
        err = file.AddProperty("unique-code", "golly-image")
        if err != nil {
            fmt.Println(err)
            return
        }
    }
    

Contributing

We welcome contributions to the Storage library! If you find a bug, have a feature request, or want to contribute improvements, please create a pull request. For major changes, please open an issue first to discuss the changes you would like to make.

Documentation

Index

Constants

View Source
const (
	GsFileScheme = "gs"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type StorageFS

type StorageFS struct {
	*vfs.BaseVFS
}

func (*StorageFS) Create

func (storageFs *StorageFS) Create(u *url.URL) (file vfs.VFile, err error)

TODO should not be allowed to create a bucket should be able to create folders and files

func (*StorageFS) Mkdir

func (storageFs *StorageFS) Mkdir(u *url.URL) (file vfs.VFile, err error)

func (*StorageFS) MkdirAll

func (storageFs *StorageFS) MkdirAll(u *url.URL) (file vfs.VFile, err error)

func (*StorageFS) Open

func (storageFs *StorageFS) Open(u *url.URL) (file vfs.VFile, err error)

Open location provided of the Storage Bucket

func (*StorageFS) Schemes

func (storageFs *StorageFS) Schemes() []string

type StorageFile

type StorageFile struct {
	*vfs.BaseFile
	// contains filtered or unexported fields
}

func (*StorageFile) AddProperty

func (storageFile *StorageFile) AddProperty(name, value string) (err error)

func (*StorageFile) Close

func (storageFile *StorageFile) Close() (err error)

func (*StorageFile) Delete

func (storageFile *StorageFile) Delete() (err error)

func (*StorageFile) GetProperty

func (storageFile *StorageFile) GetProperty(name string) (value string, err error)

func (*StorageFile) Info

func (storageFile *StorageFile) Info() (file vfs.VFileInfo, err error)

func (*StorageFile) ListAll

func (storageFile *StorageFile) ListAll() (files []vfs.VFile, err error)

func (*StorageFile) Read

func (storageFile *StorageFile) Read(b []byte) (numBytes int, err error)

func (*StorageFile) String

func (storageFile *StorageFile) String() string

func (*StorageFile) Url

func (storageFile *StorageFile) Url() *url.URL

func (*StorageFile) Write

func (storageFile *StorageFile) Write(b []byte) (numBytes int, err error)

type StorageFileInfo

type StorageFileInfo struct {
	// contains filtered or unexported fields
}

func (*StorageFileInfo) IsDir

func (f *StorageFileInfo) IsDir() bool

func (*StorageFileInfo) ModTime

func (f *StorageFileInfo) ModTime() time.Time

func (*StorageFileInfo) Mode

func (f *StorageFileInfo) Mode() os.FileMode

func (*StorageFileInfo) Name

func (f *StorageFileInfo) Name() string

func (*StorageFileInfo) Size

func (f *StorageFileInfo) Size() int64

func (*StorageFileInfo) String

func (f *StorageFileInfo) String() string

String returns a string representation of the file info.

func (*StorageFileInfo) Sys

func (f *StorageFileInfo) Sys() interface{}

type UrlOpts

type UrlOpts struct {
	Host   string
	Bucket string
	Key    string
	// contains filtered or unexported fields
}

func (*UrlOpts) CreateStorageClient

func (urlOpts *UrlOpts) CreateStorageClient() (client *storage.Client, err error)

Jump to

Keyboard shortcuts

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