Documentation ¶
Overview ¶
Package upload provides an easy and portable way to interact with CDN, buckets or blobs within any cloud/local storage location. And provides methods to read or write or upload or delete files to blob storage on GCP, AWS, Azure, in-memory, local and more.
It wraps `gocloud.dev/blob` (https://github.com/google/go-cloud/tree/master/blob) for further simplicity.
See [repo](https://github.com/Shivam010/upload) for more details
Example ¶
package main import ( "context" "fmt" "github.com/Shivam010/upload" ) const content = `Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups` func main() { ctx := context.TODO() // Different types of blob/file systems list := []string{ "", // or "mem://" (In-memory) "gs://name", // GCP cloud bucket url "file:///tmp/bin/", // Local file system url "s3://name?region=us-east-2", // S3 bucket url } for _, name := range list { // Open or create bucket using name buck, err := upload.Open(name) // or buck := upload.NewBucket(name) if err != nil { panic(err) // handle error } fmt.Println("Provider: ", buck.Provider()) // Upload a new file and get its link link, err := buck.WriteAll(ctx, "loren.ipsum", []byte(content)) if err != nil { panic(err) // handle error } fmt.Println("Link to the file (loren.ipsum)", link) // Name of the uploaded file from its link name := buck.GetName(link) // read content of the uploaded file cont, err := buck.ReadAll(ctx, name) if err != nil { panic(err) // handle error } fmt.Println("Content of the file (loren.ipsum)", cont) // delete the uploaded file if err = buck.Delete(ctx, name); err != nil { panic(err) // handle error } // Close the bucket if err = buck.Close(); err != nil { panic(err) // handle error } } }
Output:
Index ¶
- Variables
- type Bucket
- func (b *Bucket) Close() error
- func (b *Bucket) Delete(ctx context.Context, name string) error
- func (b *Bucket) GetMetadata(key string) string
- func (b *Bucket) GetName(link string) string
- func (b *Bucket) GetUrl(name string) string
- func (b *Bucket) Name() string
- func (b *Bucket) Open() error
- func (b *Bucket) OpenContext(ctx context.Context) (err error)
- func (b *Bucket) Provider() Provider
- func (b *Bucket) ReadAll(ctx context.Context, name string) ([]byte, error)
- func (b *Bucket) Reader(ctx context.Context, name string) (io.ReadCloser, error)
- func (b *Bucket) String() string
- func (b *Bucket) URL() string
- func (b *Bucket) WriteAll(ctx context.Context, name string, data []byte) (string, error)
- type Provider
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ProviderName = map[Provider]string{ InMemory: "In-Memory", FileSystem: "Local File System", GoogleCloud: "Google Cloud Console", AmazonWebServices: "Amazon Web Services", ProxiedFileSystem: "Proxied File System", }
Functions ¶
This section is empty.
Types ¶
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
func Open ¶
Open will return the blob bucket with an open bucket, it is recommended It should be used in constructor
func (*Bucket) Delete ¶
Delete will delete the file name provided from corresponding provider * name should be file name, not the http-link to get name from link use GetName method
func (*Bucket) GetMetadata ¶ added in v0.2.0
GetMetadata returns the value stored inside the key of the metadata of bucket
func (*Bucket) GetName ¶
GetName returns the actual blob key path in the bucket for provided link in the corresponding provider
func (*Bucket) GetUrl ¶
GetUrl returns the access url path for the provided name in the corresponding provider
func (*Bucket) OpenContext ¶
OpenContext opens a new bucket connection
func (*Bucket) ReadAll ¶
ReadAll will read all content of file name in bucket * name should be file name, not the http-link to get name from link use GetName method
func (*Bucket) Reader ¶
Reader will return the io.ReadCloser against the file name provided, remember to close reader * name should be file name, not the http-link to get name from link use GetName method