s3

package module
v0.0.0-...-9600de4 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2016 License: MIT Imports: 8 Imported by: 0

README

s3

s3 is a much simplified wrapper around AWS official Go SDK for the uploading and downloading to and from S3 buckets. It offers basic functions and only key id/secret authentication.

Example

Upload
func uploadExample(obj []byte) {
  conf := s3.BucketConf{
    Bucket: "s3://mybucket",
    Region: "eu-west-1",
    ID: "BADAB000",
    Secret: "24352fjkle;wkr234j5",
  }
  loc, err := s3.Upload(conf, "path/within/bucket/to/file.bin", obj)
}
Download
type objHandler struct {
  wg sync.WaitGroup
}

func (h objHandler) HandleObject(obj *s3.Object) {
  var buf bytes.Buffer
  buf.ReadFrom(obj) // *s3.Object is an io.Reader

  // do something with buf
}

func (h objHandler) OnDone() {
  h.wg.Done()
}

func exampleObject(obj []byte) {
  h := objHandler{}
  h.wg.Add(1)
  cntc, errc := s3.Download(conf, loc, h)
  select {
    case <-cntc:
	case err := <-errc:
	  log.Fatal(err)
  }

  h.wg.Wait() // Block until download is done
}

License (MIT)

Copyright (c) 2016 TV4-Gruppen AB

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Download

func Download(c BucketConf, path string, handler ObjectHandler) (<-chan int, <-chan error)

Download starts the downloading of a resource residing at path in the bucket given by the configuration.

func Upload

func Upload(c BucketConf, path string, data []byte) (string, error)

Upload starts the uploading of a binary object to the location given by path in the bucket specified by the configuration. It returns the object location in the bucket and an error value.

Types

type BucketConf

type BucketConf struct {
	Bucket, ID, Secret, Region string
}

BucketConf represents the data needed to access an S3 bucket. Specifically, it contains the bucket name, region, and key/secret credentials

type Downloader

type Downloader interface {
	Download(path string, handler ObjectHandler) (<-chan int, <-chan error)
	DownloadObjects(path string, handler ObjectHandler, nobj, startobj int) (<-chan int, <-chan error)
}

Downloader exposes two methods to download objects from an S3 bucket. Download takes as parameters a path to a resource located in the bucket, and an ObjectHandler to handle each object of the resource as it is downloaded. DownloadObjects is for testing purposes and offers a way to limit the number of objects downloaded. This can be convenient when dealing with resources containing large numbers of objects.

func NewDownloader

func NewDownloader(c BucketConf) Downloader

NewDownloader creates and initializes a Downloader for the specified bucket in the specified region.

type Object

type Object struct {
	ID int
	// contains filtered or unexported fields
}

Object is a writable structure representing a binary blob residing on an S3 bucket. It is written as part of the process of downloading objects from S3.

func (*Object) Read

func (mb *Object) Read(p []byte) (int, error)

Read reads the next len(p) bytes from the object or until the object is fully read. The return value is the number of bytes read. If the object has no data, err is io.EOF.

func (*Object) WriteAt

func (mb *Object) WriteAt(p []byte, off int64) (int, error)

WriteAt writes the buffer p to the object buffer, starting at position off. The return value is the number of bytes written.

type ObjectHandler

type ObjectHandler interface {
	HandleObject(*Object)
	OnDone()
}

ObjectHandler declares the interface needed to receive the objects downloaded by the Downloader.

type Uploader

type Uploader interface {
	Upload(data []byte, path string) (string, error)
}

Uploader is the interface that wraps the Upload method.

func NewUploader

func NewUploader(c BucketConf) Uploader

NewUploader creates and initializes a new Uploader based on the values contained in BucketConf.

Jump to

Keyboard shortcuts

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