Skip to content

Commit

Permalink
Merge pull request #8 from mobilusoss/develop
Browse files Browse the repository at this point in the history
bump
  • Loading branch information
mohemohe committed May 5, 2019
2 parents a46b26b + 9eaf2f5 commit 138d62e
Show file tree
Hide file tree
Showing 4 changed files with 434 additions and 21 deletions.
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: go
sudo: required
go:
- 1.12
- tip
services:
- docker
before_install:
- go get github.com/golang/dep/...
install:
- $GOPATH/bin/dep ensure
before_script:
- docker run -d --name minio -e 'MINIO_ACCESS_KEY=accesskey' -e 'MINIO_SECRET_KEY=secretkey' -p 9000:9000 minio/minio:RELEASE.2018-12-27T18-33-08Z server /data
script:
- go test -race -coverprofile=coverage.txt -covermode=atomic
after_success:
- bash <(curl -s https://codecov.io/bash)
123 changes: 123 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
go-s3fs
====

[![Build Status](https://travis-ci.org/mobilusoss/go-s3fs.svg?branch=master)](https://travis-ci.org/mobilusoss/go-s3fs)
[![codecov](https://codecov.io/gh/mobilusoss/go-s3fs/branch/master/graph/badge.svg)](https://codecov.io/gh/mobilusoss/go-s3fs)
[![Go Report Card](https://goreportcard.com/badge/github.com/mobilusoss/go-s3fs)](https://goreportcard.com/report/github.com/mobilusoss/go-s3fs)
[![codebeat badge](https://codebeat.co/badges/e03e3de0-9d71-43ce-a6ac-8e0c6445485a)](https://codebeat.co/projects/github-com-mobilusoss-go-s3fs-master)
![GitHub](https://img.shields.io/github/license/mobilusoss/go-s3fs.svg)

Amazon S3 wrapper for human

<!-- toc -->

## Overview

AWS offers a complex and bizarre SDK. We needed a library that would make it easier and faster to use S3.
go-s3fs solves this problem. It wraps the official SDK, making S3 extremely easy to use.

## Installation

```bash
go get -u github.com/mobilusoss/go-s3fs
```

## Usage

### Connect to S3 using IAM role

```go
package main

import (
"fmt"
"github.com/mobilusoss/go-s3fs"
)

func main() {
fs := s3fs.New(&s3fs.Config{
Bucket: "samplebucket",
})
readCloser, err := fs.Get("/file.txt")
if err != nil {
panic("s3 error")
}
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(*readCloser); err != nil {
panic("io error")
}
text := buf.String()
fmt.Println(text)
}
```

### Connect to [MinIO](https://github.com/minio/minio)

```go
package main

import (
"fmt"
"github.com/mobilusoss/go-s3fs"
)

func main() {
fs := s3fs.New(&s3fs.Config{
Bucket: "samplebucket",
EnableMinioCompat: true,
Endpoint: "http://127.0.0.1:9000",
EnableIAMAuth: true,
AccessKeyID: "accesskey",
AccessSecretKey: "secretkey",
})
readCloser, err := fs.Get("/file.txt")
if err != nil {
panic("s3 error")
}
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(*readCloser); err != nil {
panic("io error")
}
text := buf.String()
fmt.Println(text)
}
```

### Can be logically divided tenant

```go
package main

import (
"github.com/mobilusoss/go-s3fs"
)

func main() {
_ = s3fs.New(&s3fs.Config{
Bucket: "samplebucket",
Domain: "tenantone",
})
}
```

### Can be logically divided tenant per application

```go
package main

import (
"github.com/mobilusoss/go-s3fs"
)

func main() {
_ = s3fs.New(&s3fs.Config{
Bucket: "samplebucket",
Namespace: "appone",
Domain: "tenantone",
})
}
```

## License

MIT
69 changes: 48 additions & 21 deletions s3fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ type (
config *Config
}
Config struct {
NameSpace string
Domain string
Region string
Bucket string
EnableIAMAuth bool
AccessKeyID string
AccessSecretKey string
NameSpace string
Domain string
Region string
Bucket string
EnableIAMAuth bool
AccessKeyID string
AccessSecretKey string
EnableMinioCompat bool
Endpoint string
}
FileInfo struct {
Name string `json:"name"`
Expand All @@ -48,25 +50,29 @@ const (
)

func New(config *Config) *S3FS {
var sess *session.Session
if config.Region == "" {
config.Region = endpoints.ApNortheast1RegionID
}

options := session.Options{
Config: aws.Config{
Region: aws.String(config.Region),
},
}
if config.EnableIAMAuth {
sess = session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String(config.Region),
Credentials: credentials.NewStaticCredentials(config.AccessKeyID, config.AccessSecretKey, ""),
},
SharedConfigState: session.SharedConfigDisable,
}))
} else {
sess = session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String(config.Region),
},
}))
options.Config.Credentials = credentials.NewStaticCredentials(config.AccessKeyID, config.AccessSecretKey, "")
options.SharedConfigState = session.SharedConfigDisable
}

if config.EnableMinioCompat {
options.Config.S3ForcePathStyle = aws.Bool(config.EnableMinioCompat)
}

if config.Endpoint != "" {
options.Config.Endpoint = aws.String(config.Endpoint)
}

sess := session.Must(session.NewSessionWithOptions(options))
serv := s3.New(sess)

return &S3FS{
Expand All @@ -76,6 +82,27 @@ func New(config *Config) *S3FS {
}
}

func (this *S3FS) CreateBucket(name string) error {
_, err := this.s3.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(name),
})
if err != nil {
return err
}

err = this.s3.WaitUntilBucketExists(&s3.HeadBucketInput{
Bucket: aws.String(name),
})
return err
}

func (this *S3FS) DeleteBucket(name string) error {
_, err := this.s3.DeleteBucket(&s3.DeleteBucketInput{
Bucket: aws.String(name),
})
return err
}

func (this *S3FS) List(key string) *[]FileInfo {
fileList := make([]FileInfo, 0)
var continuationToken *string
Expand Down
Loading

0 comments on commit 138d62e

Please sign in to comment.