Giter Club home page Giter Club logo

go-cloudfile's Introduction

GoDoc Build Status Coverage Status

cloudfile

A consistent way to work with remote (and local) files.

func main() {
	url := "s3://my-bucket/path/to/file"
	// url := "http://example.com/path/to/file"
	// url := "/path/to/file"

	r, err := cloudfile.Open(url)
	if err != nil {
		log.Fatal(err)
	}
	defer r.Close()

	...
}

Use cloudfile to write code that can doesn't care whether its data comes from the local filesystem or a remote storage provider. All the information necessary to locate a file is specified as a URI string.

Motivation

Suppose you're writing a server that loads a data file. In the first version you just load the data from the local filesystem:

./myserver --data /path/to/data

Later you want to load the data from an S3 bucket. Typically this would require rewriting a bunch of code to load data using the AWS API rather than os.Open, after which you would no longer be able to load from a local file without changing the code back.

Using cloudfile.Open, you would simply pass in S3 URL and there would be no need for any code changes:

./myserver --data s3://my-bucket/path/to/data

Backends

cloudfile currently supports the following backends:

Amazon S3

URLs look like s3://BUCKET/KEY

Credentials can be specified by:

  1. setting the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables

  2. creating ~/.aws/credentials with the following contents:

    [default]
    aws_access_key_id=YOUR_ACCESS_KEY
    aws_secret_access_key=YOUR_SECRET_KEY
    
  3. initializing the driver explicitly

    import (
    	"github.com/alexflint/go-cloudfile"
    	"github.com/alexflint/go-cloudfile/s3file"
    )
    
    func main() {
    	cloudfile.Drivers["s3:"] = s3file.NewDriver("ACCESS_KEY", "SECRET_KEY", aws.USWest)
    	...
    }

HTTP and HTTPS

Uses standard HTTP URLs: https://example.com/path/to/resource.

The resource is fetched with a GET using the default HTTP client, http.DefaultClient. HTTP resources are read-only.

Local paths

Any path that does not have a known protocol prefix ("s3:", "http:", "https:", etc) is interpreted as a local path and behavior will be equivalent to os.Open, ioutil.ReadFile, or ioutil.WriteFile.

Custom backends

You can define your own backend by implementing cloudfile.Driver:

type InMemoryDriver map[string][]byte

func (d InMemoryDriver) Open(path string) (io.ReadCloser, error) {
	return ioutil.NopCloser(bytes.NewBuffer(d[path])), nil
}

func (d InMemoryDriver) ReadFile(path string) ([]byte, error) {
	return d[path], nil
}

func (d InMemoryDriver) WriteFile(path string, buf []byte) error {
	d[path] = buf
	return nil
}

Register the driver with:

cloudfile.Drivers["inmemory:"] = make(InMemoryDriver)

Then use it with:

cloudfile.WriteFile("inmemory:foo", []byte("contents of file"))
buf, err := cloudfile.ReadFile("inmemory:foo")
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(string(buf))

go-cloudfile's People

Contributors

alexflint avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.