upload

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2022 License: MIT Imports: 12 Imported by: 0

README

upload

Build Go Report Card Go Reference License GitHub release

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.

Proxied File system with HTTP Router

You can store the files in using file system blob and can serve them using http file server. Upload library provides everything you need to do that in-built. Just use pfsblob as your file system handler. See the example in pfsblob/tests.

URL format for pfsblob
  1. URL Scheme: pfs
  2. URL Host: Your domain name (without protocol) e.g. localhost:8080 or example.com
  3. URL Path: Your storage directory in which you want to manage your files e.g. /tmp/files
  4. Domain Protocol: If your domain uses secure https protocol, set a query parameter: ?secure=true
  5. Route: By default, files will be served at the root of the domain, if you want to change it, use query parameter: ?route=static

Complete Query will look something like these:

  1. pfs://localhost:8080/tmp/files?secure=false
    This will serve files at http://localhost:8080/...
    e.g. the link for the file /tmp/files/image.png will be http://localhost:8080/image.png

  2. pfs://example.com/tmp/files?secure=true&route=public
    This will serve files at https://example.com/public/...
    e.g. the link for the file /tmp/files/image.png will be https://example.com/public/image.png

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.`

func main() {
	ctx := context.TODO()
	s3Url := "s3://name?region=us-east-2"
	// Open or create bucket using name
	buck, err := upload.Open(s3Url) // or buck := upload.NewBucket(s3Url)
	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
	}
}

License

This project is licensed under the MIT License

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

Examples

Constants

This section is empty.

Variables

View Source
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 NewBucket

func NewBucket(bucket string) *Bucket

NewBucket will return the blob bucket using the provided bucket url

func Open

func Open(bucket string) (*Bucket, error)

Open will return the blob bucket with an open bucket, it is recommended It should be used in constructor

func (*Bucket) Close

func (b *Bucket) Close() error

Close opens a new bucket connection

func (*Bucket) Delete

func (b *Bucket) Delete(ctx context.Context, name string) error

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

func (b *Bucket) GetMetadata(key string) string

GetMetadata returns the value stored inside the key of the metadata of bucket

func (*Bucket) GetName

func (b *Bucket) GetName(link string) string

GetName returns the actual blob key path in the bucket for provided link in the corresponding provider

func (*Bucket) GetUrl

func (b *Bucket) GetUrl(name string) string

GetUrl returns the access url path for the provided name in the corresponding provider

func (*Bucket) Name

func (b *Bucket) Name() string

Name returns name of the bucket

func (*Bucket) Open

func (b *Bucket) Open() error

Open opens a new bucket connection

func (*Bucket) OpenContext

func (b *Bucket) OpenContext(ctx context.Context) (err error)

OpenContext opens a new bucket connection

func (*Bucket) Provider

func (b *Bucket) Provider() Provider

Provider returns the type of service provider in use

func (*Bucket) ReadAll

func (b *Bucket) ReadAll(ctx context.Context, name string) ([]byte, error)

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

func (b *Bucket) Reader(ctx context.Context, name string) (io.ReadCloser, error)

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

func (*Bucket) String added in v0.2.0

func (b *Bucket) String() string

String implements stringer on Bucket

func (*Bucket) URL added in v0.2.0

func (b *Bucket) URL() string

URL returns the url of the bucket

func (*Bucket) WriteAll

func (b *Bucket) WriteAll(ctx context.Context, name string, data []byte) (string, error)

WriteAll will upload the content of data, under the provided path/name in name and returns the corresponding access url or error if any

type Provider

type Provider int

Provider describes the information about the bucket service provider

const (
	InMemory Provider = iota
	FileSystem
	GoogleCloud
	AmazonWebServices
	ProxiedFileSystem
)

func (Provider) String

func (p Provider) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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