Compare commits

...

17 Commits

Author SHA1 Message Date
dependabot[bot]
7d95149519 Bump github.com/aws/aws-sdk-go-v2/config in /dynamodb (#339)
Bumps [github.com/aws/aws-sdk-go-v2/config](https://github.com/aws/aws-sdk-go-v2) from 1.13.1 to 1.15.0.
- [Release notes](https://github.com/aws/aws-sdk-go-v2/releases)
- [Changelog](https://github.com/aws/aws-sdk-go-v2/blob/main/CHANGELOG.md)
- [Commits](https://github.com/aws/aws-sdk-go-v2/compare/config/v1.13.1...v1.15.0)

---
updated-dependencies:
- dependency-name: github.com/aws/aws-sdk-go-v2/config
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-03-09 08:26:56 +01:00
dependabot[bot]
1c100fe50a Bump github.com/aws/aws-sdk-go-v2/credentials in /dynamodb (#336)
Bumps [github.com/aws/aws-sdk-go-v2/credentials](https://github.com/aws/aws-sdk-go-v2) from 1.8.0 to 1.10.0.
- [Release notes](https://github.com/aws/aws-sdk-go-v2/releases)
- [Changelog](https://github.com/aws/aws-sdk-go-v2/blob/main/CHANGELOG.md)
- [Commits](https://github.com/aws/aws-sdk-go-v2/compare/v1.8.0...v1.10.0)

---
updated-dependencies:
- dependency-name: github.com/aws/aws-sdk-go-v2/credentials
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-03-08 09:33:01 +01:00
dependabot[bot]
395ea51b28 Bump github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue (#337)
Bumps [github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue](https://github.com/aws/aws-sdk-go-v2) from 1.6.0 to 1.8.0.
- [Release notes](https://github.com/aws/aws-sdk-go-v2/releases)
- [Changelog](https://github.com/aws/aws-sdk-go-v2/blob/main/CHANGELOG.md)
- [Commits](https://github.com/aws/aws-sdk-go-v2/compare/v1.6.0...v1.8.0)

---
updated-dependencies:
- dependency-name: github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-03-08 09:10:03 +01:00
dependabot[bot]
d327e9b0dd Bump github.com/aws/aws-sdk-go-v2/service/dynamodb in /dynamodb (#335)
Bumps [github.com/aws/aws-sdk-go-v2/service/dynamodb](https://github.com/aws/aws-sdk-go-v2) from 1.13.0 to 1.15.0.
- [Release notes](https://github.com/aws/aws-sdk-go-v2/releases)
- [Changelog](https://github.com/aws/aws-sdk-go-v2/blob/main/CHANGELOG.md)
- [Commits](https://github.com/aws/aws-sdk-go-v2/compare/v1.13.0...v1.15.0)

---
updated-dependencies:
- dependency-name: github.com/aws/aws-sdk-go-v2/service/dynamodb
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-03-08 08:29:55 +01:00
dependabot[bot]
5cbf98de49 Bump go.mongodb.org/mongo-driver from 1.8.3 to 1.8.4 in /mongodb (#334)
Bumps [go.mongodb.org/mongo-driver](https://github.com/mongodb/mongo-go-driver) from 1.8.3 to 1.8.4.
- [Release notes](https://github.com/mongodb/mongo-go-driver/releases)
- [Commits](https://github.com/mongodb/mongo-go-driver/compare/v1.8.3...v1.8.4)

---
updated-dependencies:
- dependency-name: go.mongodb.org/mongo-driver
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-03-08 08:29:28 +01:00
dependabot[bot]
22444f8aa9 Bump github.com/mattn/go-sqlite3 from 1.14.11 to 1.14.12 in /sqlite3 (#333)
Bumps [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3) from 1.14.11 to 1.14.12.
- [Release notes](https://github.com/mattn/go-sqlite3/releases)
- [Commits](https://github.com/mattn/go-sqlite3/compare/v1.14.11...v1.14.12)

---
updated-dependencies:
- dependency-name: github.com/mattn/go-sqlite3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-03-08 08:29:20 +01:00
M. Efe Çetin
cc5ccf062b 🚀 feature: add bbolt implementation (#321)
* Update CI/CD tests.

* Add Bbolt db.

* Add README.

* Add actions.

* Drop go1.14 & go1.15 for bbolt.

* Add support for go1.16

* Update config.go

Co-authored-by: wernerr <rene.werner@verivox.com>
2022-02-10 15:45:13 +01:00
M. Efe Çetin
315f14ce58 🔥 Make DynamoDB Production-Ready [🎌 breaking change] (#323)
* Update CI/CD tests.

* Update aws-sdk-go to v2, add unit tests, remove warning test, make config better.

* add action

Co-authored-by: wernerr <rene.werner@verivox.com>
2022-02-10 15:44:59 +01:00
wernerr
f9eaa6ae4d stabilize ristretto tests
bug already communicated
2022-02-10 15:23:44 +01:00
dependabot[bot]
2be6e0cc10 Bump github.com/aws/aws-sdk-go from 1.42.48 to 1.42.50 in /dynamodb (#327)
Bumps [github.com/aws/aws-sdk-go](https://github.com/aws/aws-sdk-go) from 1.42.48 to 1.42.50.
- [Release notes](https://github.com/aws/aws-sdk-go/releases)
- [Changelog](https://github.com/aws/aws-sdk-go/blob/main/CHANGELOG.md)
- [Commits](https://github.com/aws/aws-sdk-go/compare/v1.42.48...v1.42.50)

---
updated-dependencies:
- dependency-name: github.com/aws/aws-sdk-go
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-02-10 14:23:04 +01:00
dependabot[bot]
5b444c36df Bump github.com/aws/aws-sdk-go from 1.42.46 to 1.42.48 in /dynamodb (#325)
Bumps [github.com/aws/aws-sdk-go](https://github.com/aws/aws-sdk-go) from 1.42.46 to 1.42.48.
- [Release notes](https://github.com/aws/aws-sdk-go/releases)
- [Changelog](https://github.com/aws/aws-sdk-go/blob/main/CHANGELOG.md)
- [Commits](https://github.com/aws/aws-sdk-go/compare/v1.42.46...v1.42.48)

---
updated-dependencies:
- dependency-name: github.com/aws/aws-sdk-go
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-02-08 08:42:04 +01:00
M. Efe Çetin
61fa9709f2 🔥 several updates for s3 (#322)
* updates for s3.

* fix linter
2022-02-04 21:53:36 +01:00
Paul Cento
45cc6b9dcd 🚀 Add S3 Implementation (#227)
* implement basic s3 operations

* add request timeout contexts

* remove close channel

* update Config comments

* fix README

* stuff

* set config options in s3.Storage

* add s3 workflow for github actions

* Bump SDK to aws/aws-sdk-go-v2

* update

* Add missing README.

* Add security workflows for s3.

* Add tests for S3

* update

update

update

update

update

update

update

update

update

update

update

update

update

update

* update

* Code reviews.

Co-authored-by: Paul Cento <prscento@uwaterloo.ca>
Co-authored-by: Alex Bakker <abakks@hotmail.com>
Co-authored-by: Muhammed Efe Çetin <efectn@protonmail.com>
2022-02-04 12:41:26 +01:00
dependabot[bot]
e7293b3841 Bump github.com/mattn/go-sqlite3 from 1.14.10 to 1.14.11 in /sqlite3 (#319)
Bumps [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3) from 1.14.10 to 1.14.11.
- [Release notes](https://github.com/mattn/go-sqlite3/releases)
- [Commits](https://github.com/mattn/go-sqlite3/compare/v1.14.10...v1.14.11)

---
updated-dependencies:
- dependency-name: github.com/mattn/go-sqlite3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-02-04 10:02:37 +01:00
dependabot[bot]
69b0b5a26f Bump github.com/aws/aws-sdk-go from 1.42.45 to 1.42.46 in /dynamodb (#320)
Bumps [github.com/aws/aws-sdk-go](https://github.com/aws/aws-sdk-go) from 1.42.45 to 1.42.46.
- [Release notes](https://github.com/aws/aws-sdk-go/releases)
- [Changelog](https://github.com/aws/aws-sdk-go/blob/main/CHANGELOG.md)
- [Commits](https://github.com/aws/aws-sdk-go/compare/v1.42.45...v1.42.46)

---
updated-dependencies:
- dependency-name: github.com/aws/aws-sdk-go
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-02-04 10:01:47 +01:00
dependabot[bot]
edb920dc05 Bump github.com/aws/aws-sdk-go from 1.42.38 to 1.42.45 in /dynamodb (#318)
Bumps [github.com/aws/aws-sdk-go](https://github.com/aws/aws-sdk-go) from 1.42.38 to 1.42.45.
- [Release notes](https://github.com/aws/aws-sdk-go/releases)
- [Changelog](https://github.com/aws/aws-sdk-go/blob/main/CHANGELOG.md)
- [Commits](https://github.com/aws/aws-sdk-go/compare/v1.42.38...v1.42.45)

---
updated-dependencies:
- dependency-name: github.com/aws/aws-sdk-go
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-02-03 16:10:11 +01:00
dependabot[bot]
df9fdcbaf9 Bump go.mongodb.org/mongo-driver from 1.8.2 to 1.8.3 in /mongodb (#317)
Bumps [go.mongodb.org/mongo-driver](https://github.com/mongodb/mongo-go-driver) from 1.8.2 to 1.8.3.
- [Release notes](https://github.com/mongodb/mongo-go-driver/releases)
- [Commits](https://github.com/mongodb/mongo-go-driver/compare/v1.8.2...v1.8.3)

---
updated-dependencies:
- dependency-name: go.mongodb.org/mongo-driver
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-02-03 16:10:03 +01:00
30 changed files with 1496 additions and 180 deletions

View File

@@ -110,3 +110,21 @@ updates:
automerged_updates:
- match:
dependency_name: "gofiber/fiber/*"
- package-ecosystem: "gomod"
directory: "/s3/" # Location of package manifests
default_labels:
- "🤖 Dependencies"
schedule:
interval: "daily"
automerged_updates:
- match:
dependency_name: "gofiber/fiber/*"
- package-ecosystem: "gomod"
directory: "/bbolt/" # Location of package manifests
default_labels:
- "🤖 Dependencies"
schedule:
interval: "daily"
automerged_updates:
- match:
dependency_name: "gofiber/fiber/*"

View File

@@ -16,7 +16,7 @@ jobs:
go get -u github.com/securego/gosec/v2/cmd/gosec
- name: Run Gosec (root)
working-directory: .
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal -exclude-dir=arangodb -exclude-dir=badger -exclude-dir=dynamodb -exclude-dir=memcache -exclude-dir=memory -exclude-dir=mongodb -exclude-dir=mysql -exclude-dir=postgres -exclude-dir=redis -exclude-dir=ristretto -exclude-dir=sqlite3 ./..."
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal -exclude-dir=arangodb -exclude-dir=badger -exclude-dir=dynamodb -exclude-dir=memcache -exclude-dir=memory -exclude-dir=mongodb -exclude-dir=mysql -exclude-dir=postgres -exclude-dir=redis -exclude-dir=ristretto -exclude-dir=sqlite3 -exclude-dir=s3 -exclude-dir=bbolt ./..."
# -----
- name: Run Gosec (arangodb)
working-directory: ./arangodb
@@ -58,7 +58,15 @@ jobs:
working-directory: ./sqlite3
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
# -----
- name: Run Gosec (s3)
working-directory: ./s3
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
# -----
- name: Run Gosec (ristretto)
working-directory: ./ristretto
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
# -----
- name: Run Gosec (bbolt)
working-directory: ./bbolt
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
# -----

24
.github/workflows/test-bbolt.yml vendored Normal file
View File

@@ -0,0 +1,24 @@
'on':
- push
- pull_request
name: Bbolt
jobs:
Tests:
runs-on: ubuntu-latest
strategy:
matrix:
go-version:
- 1.16.x
- 1.17.x
platform:
- ubuntu-latest
- windows-latest
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: '${{ matrix.go-version }}'
- name: Fetch Repository
uses: actions/checkout@v2
- name: Run Test
run: cd ./bbolt && go mod tidy && go test ./... -v -race

31
.github/workflows/test-dynamodb.yml vendored Normal file
View File

@@ -0,0 +1,31 @@
'on':
- push
- pull_request
name: DynamoDB
jobs:
Tests:
runs-on: ubuntu-latest
services:
mongo:
image: 'amazon/dynamodb-local:latest'
ports:
- '8000:8000'
strategy:
matrix:
go-version:
- 1.14.x
- 1.15.x
- 1.16.x
- 1.17.x
platform:
- ubuntu-latest
- windows-latest
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: '${{ matrix.go-version }}'
- name: Fetch Repository
uses: actions/checkout@v2
- name: Run Test
run: cd ./dynamodb && go test ./... -v -race

35
.github/workflows/test-s3.yml vendored Normal file
View File

@@ -0,0 +1,35 @@
'on':
- push
- pull_request
name: S3
jobs:
Tests:
runs-on: ubuntu-latest
strategy:
matrix:
go-version:
- 1.14.x
- 1.15.x
- 1.16.x
- 1.17.x
platform:
- ubuntu-latest
- windows-latest
steps:
- name: Install MinIO
run: |
docker run -d -p 9000:9000 --name minio minio/minio server /data
export AWS_ACCESS_KEY_ID=minioadmin
export AWS_SECRET_ACCESS_KEY=minioadmin
export AWS_EC2_METADATA_DISABLED=true
aws --endpoint-url http://127.0.0.1:9000/ s3 mb s3://testbucket
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: '${{ matrix.go-version }}'
- name: Fetch Repository
uses: actions/checkout@v2
- name: Run Test
run: cd ./s3 && go test ./... -v -race

View File

@@ -81,3 +81,9 @@ type Storage interface {
* [SQLite3](/sqlite3) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Local+Storage%22">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Local%20Storage?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
* [S3](/s3) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22S3%22">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/S3?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
* [Bbolt](/bbolt) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Bbolt%22">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Bbolt?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>

92
bbolt/README.md Normal file
View File

@@ -0,0 +1,92 @@
# Bbolt
A Bbolt storage driver using [etcd-io/bbolt](https://github.com/etcd-io/bbolt). Bolt is a pure Go key/value store inspired by [Howard Chu's](https://twitter.com/hyc_symas) [LMDB project](https://www.symas.com/symas-embedded-database-lmdb). The goal of the project is to provide a simple, fast, and reliable database for projects that don't require a full database server such as Postgres or MySQL.
### Table of Contents
- [Signatures](#signatures)
- [Installation](#installation)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)
### Signatures
```go
func New(config ...Config) Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
```
### Installation
Bbolt is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
```bash
go mod init github.com/<user>/<repo>
```
And then install the s3 implementation:
```bash
go get github.com/gofiber/storage/bbolt
```
### Examples
Import the storage package.
```go
import "github.com/gofiber/storage/bbolt"
```
You can use the following possibilities to create a storage:
```go
// Initialize default config
store := bbolt.New()
// Initialize custom config
store := bbolt.New(bbolt.Config{
Database: "my_database.db",
Bucket: "my-bucket",
Reset: false,
})
```
### Config
```go
// Config defines the config for storage.
type Config struct {
// Database path
//
// Optional. Default is "fiber.db"
Database string
// Bbolt bucket name
//
// Optional. Default is "fiber_storage"
Bucket string
// Timeout is the amount of time to wait to obtain a file lock.
// Only available on Darwin and Linux.
//
// Optional. Default is 0 (no timeout)
Timeout time.Duration
// Open database in read-only mode.
//
// Optional. Default is false
ReadOnly bool
// Reset clears any existing keys in existing Bucket
//
// Optional. Default is false
Reset bool
}
```
### Default Config
```go
// ConfigDefault is the default config
var ConfigDefault = Config{
Database: "fiber.db",
Bucket: "fiber_storage",
Timeout: 0,
ReadOnly: false,
Reset: false,
}
```

106
bbolt/bbolt.go Normal file
View File

@@ -0,0 +1,106 @@
package bbolt
import (
"time"
"github.com/gofiber/utils"
"go.etcd.io/bbolt"
)
// Storage interface that is implemented by storage providers
type Storage struct {
conn *bbolt.DB
bucket string
}
// New creates a new storage
func New(config ...Config) *Storage {
// Set default config
cfg := configDefault(config...)
conn, err := bbolt.Open(cfg.Database, 0666, &bbolt.Options{
Timeout: cfg.Timeout,
ReadOnly: cfg.ReadOnly,
})
if err != nil {
panic(err)
}
// Reset bucket if field selected
if cfg.Reset {
if err := removeBucket(cfg, conn); err != nil {
panic(err)
}
}
// Create bucket if not exists
if err := createBucket(cfg, conn); err != nil {
panic(err)
}
return &Storage{
conn: conn,
bucket: cfg.Bucket,
}
}
// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 {
return nil, nil
}
var value []byte
err := s.conn.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(utils.UnsafeBytes(s.bucket))
value = b.Get(utils.UnsafeBytes(key))
return nil
})
return value, err
}
// Set key with value
func (s *Storage) Set(key string, value []byte, exp time.Duration) error {
if len(key) <= 0 || len(value) <= 0 {
return nil
}
return s.conn.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket(utils.UnsafeBytes(s.bucket))
return b.Put(utils.UnsafeBytes(key), value)
})
}
// Delete entry by key
func (s *Storage) Delete(key string) error {
if len(key) <= 0 {
return nil
}
return s.conn.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket(utils.UnsafeBytes(s.bucket))
return b.Delete(utils.UnsafeBytes(key))
})
}
// Reset all entries
func (s *Storage) Reset() error {
return s.conn.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket(utils.UnsafeBytes(s.bucket))
return b.ForEach(func(k, _ []byte) error {
return b.Delete(k)
})
})
}
// Close the database
func (s *Storage) Close() error {
return s.conn.Close()
}

97
bbolt/bbolt_test.go Normal file
View File

@@ -0,0 +1,97 @@
package bbolt
import (
"testing"
"github.com/gofiber/utils"
)
var testStore = New()
func Test_Bbolt_Set(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
}
func Test_Bbolt_Set_Override(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
err = testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
}
func Test_Bbolt_Get(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, val, result)
}
func Test_Bbolt_Get_NotExist(t *testing.T) {
result, err := testStore.Get("notexist")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(result) == 0)
}
func Test_Bbolt_Delete(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
err = testStore.Delete(key)
utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(result) == 0)
}
func Test_Bbolt_Reset(t *testing.T) {
var (
val = []byte("doe")
)
err := testStore.Set("john1", val, 0)
utils.AssertEqual(t, nil, err)
err = testStore.Set("john2", val, 0)
utils.AssertEqual(t, nil, err)
err = testStore.Reset()
utils.AssertEqual(t, nil, err)
result, err := testStore.Get("john1")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(result) == 0)
result, err = testStore.Get("john2")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(result) == 0)
}
func Test_Bbolt_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close())
}

63
bbolt/config.go Normal file
View File

@@ -0,0 +1,63 @@
package bbolt
import "time"
// Config defines the config for storage.
type Config struct {
// Database path
//
// Optional. Default is "fiber.db"
Database string
// Bbolt bucket name
//
// Optional. Default is "fiber_storage"
Bucket string
// Timeout is the amount of time to wait to obtain a file lock.
// Only available on Darwin and Linux.
//
// Optional. Default is 0 (no timeout)
Timeout time.Duration
// Open database in read-only mode.
//
// Optional. Default is false
ReadOnly bool
// Reset clears any existing keys in existing Bucket
//
// Optional. Default is false
Reset bool
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Database: "fiber.db",
Bucket: "fiber_storage",
Timeout: 0,
ReadOnly: false,
Reset: false,
}
// Helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}
// Override default config
cfg := config[0]
// Set default values
if cfg.Database == "" {
cfg.Database = ConfigDefault.Database
}
if cfg.Bucket == "" {
cfg.Bucket = ConfigDefault.Bucket
}
return cfg
}

10
bbolt/go.mod Normal file
View File

@@ -0,0 +1,10 @@
module github.com/gofiber/storage/bbolt
go 1.16
require (
github.com/gofiber/utils v0.1.2
go.etcd.io/bbolt v1.3.6
)
require golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a // indirect

7
bbolt/go.sum Normal file
View File

@@ -0,0 +1,7 @@
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU=
go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4=
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a h1:ppl5mZgokTT8uPkmYOyEUmPTr3ypaKkg5eFOGrAmxxE=
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

20
bbolt/utils.go Normal file
View File

@@ -0,0 +1,20 @@
package bbolt
import (
"github.com/gofiber/utils"
"go.etcd.io/bbolt"
)
func createBucket(cfg Config, conn *bbolt.DB) error {
return conn.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(utils.UnsafeBytes(cfg.Bucket))
return err
})
}
func removeBucket(cfg Config, conn *bbolt.DB) error {
return conn.Update(func(tx *bbolt.Tx) error {
return tx.DeleteBucket(utils.UnsafeBytes(cfg.Bucket))
})
}

View File

@@ -1,4 +1,7 @@
# DynamoDB is still in development, do not use in production!
# DynamoDB
A DynamoDB storage driver using [aws/aws-sdk-go-v2](https://github.com/aws/aws-sdk-go-v2).
**Note:** If config fields of credentials not given, credentials are using from the environment variables, ~/.aws/credentials, or EC2 instance role. If config fields of credentials given, credentials are using from config. Look at: [specifying credentials](https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials)
....
@@ -60,28 +63,70 @@ type Config struct {
// Optional ("fiber_storage" by default).
Table string
// AWS access key ID (part of the credentials).
// Optional (read from shared credentials file or environment variable if not set).
// Environment variable: "AWS_ACCESS_KEY_ID".
AWSaccessKeyID string
// AWS secret access key (part of the credentials).
// Optional (read from shared credentials file or environment variable if not set).
// Environment variable: "AWS_SECRET_ACCESS_KEY".
AWSsecretAccessKey string
// CustomEndpoint allows you to set a custom DynamoDB service endpoint.
// This is especially useful if you're running a "DynamoDB local" Docker container for local testing.
// Typical value for the Docker container: "http://localhost:8000".
// See https://hub.docker.com/r/amazon/dynamodb-local/.
// Optional ("" by default)
CustomEndpoint string
Endpoint string
// Credentials overrides AWS access key and AWS secret access key. Not recommended.
//
// Optional. Default is Credentials{}
Credentials Credentials
// The maximum number of times requests that encounter retryable failures should be attempted.
//
// Optional. Default is 3
MaxAttempts int
// Reset clears any existing keys in existing Bucket
//
// Optional. Default is false
Reset bool
// ReadCapacityUnits of the table.
// Only required when the table doesn't exist yet and is created by gokv.
// Optional (5 by default, which is the same default value as when creating a table in the web console)
// 25 RCUs are included in the free tier (across all tables).
// For example calculations, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/HowItWorks.ProvisionedThroughput.
// For limits, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/Limits.md#capacity-units-and-provisioned-throughput.md#provisioned-throughput.
ReadCapacityUnits int64
// ReadCapacityUnits of the table.
// Only required when the table doesn't exist yet and is created by gokv.
// Optional (5 by default, which is the same default value as when creating a table in the web console)
// 25 RCUs are included in the free tier (across all tables).
// For example calculations, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/HowItWorks.ProvisionedThroughput.
// For limits, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/Limits.md#capacity-units-and-provisioned-throughput.md#provisioned-throughput.
WriteCapacityUnits int64
// If the table doesn't exist yet, gokv creates it.
// If WaitForTableCreation is true, gokv will block until the table is created, with a timeout of 15 seconds.
// If the table still doesn't exist after 15 seconds, an error is returned.
// If WaitForTableCreation is false, gokv returns the client immediately.
// In the latter case you need to make sure that you don't read from or write to the table before it's created,
// because otherwise you will get ResourceNotFoundException errors.
// Optional (true by default).
WaitForTableCreation *bool
}
type Credentials struct {
AccessKey string
SecretAccessKey string
}
```
### Default Config
```go
var ConfigDefault = Config{
Table: "fiber_storage",
Table: "fiber_storage",
Credentials: Credentials{},
MaxAttempts: 3,
Reset: false,
ReadCapacityUnits: 5,
WriteCapacityUnits: 5,
WaitForTableCreation: aws.Bool(true),
}
```

View File

@@ -1,6 +1,6 @@
package dynamodb
import "github.com/aws/aws-sdk-go/aws"
import "github.com/aws/aws-sdk-go-v2/aws"
// Config defines the config for storage.
type Config struct {
@@ -15,22 +15,27 @@ type Config struct {
// Optional ("fiber_storage" by default).
Table string
// AWS access key ID (part of the credentials).
// Optional (read from shared credentials file or environment variable if not set).
// Environment variable: "AWS_ACCESS_KEY_ID".
AWSaccessKeyID string
// AWS secret access key (part of the credentials).
// Optional (read from shared credentials file or environment variable if not set).
// Environment variable: "AWS_SECRET_ACCESS_KEY".
AWSsecretAccessKey string
// CustomEndpoint allows you to set a custom DynamoDB service endpoint.
// This is especially useful if you're running a "DynamoDB local" Docker container for local testing.
// Typical value for the Docker container: "http://localhost:8000".
// See https://hub.docker.com/r/amazon/dynamodb-local/.
// Optional ("" by default)
CustomEndpoint string
Endpoint string
// Credentials overrides AWS access key and AWS secret access key. Not recommended.
//
// Optional. Default is Credentials{}
Credentials Credentials
// The maximum number of times requests that encounter retryable failures should be attempted.
//
// Optional. Default is 3
MaxAttempts int
// Reset clears any existing keys in existing Bucket
//
// Optional. Default is false
Reset bool
// ReadCapacityUnits of the table.
// Only required when the table doesn't exist yet and is created by gokv.
@@ -38,14 +43,16 @@ type Config struct {
// 25 RCUs are included in the free tier (across all tables).
// For example calculations, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/HowItWorks.ProvisionedThroughput.
// For limits, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/Limits.md#capacity-units-and-provisioned-throughput.md#provisioned-throughput.
readCapacityUnits int64
ReadCapacityUnits int64
// ReadCapacityUnits of the table.
// Only required when the table doesn't exist yet and is created by gokv.
// Optional (5 by default, which is the same default value as when creating a table in the web console)
// 25 RCUs are included in the free tier (across all tables).
// For example calculations, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/HowItWorks.ProvisionedThroughput.
// For limits, see https://github.com/awsdocs/amazon-dynamodb-developer-guide/blob/c420420a59040c5b3dd44a6e59f7c9e55fc922ef/doc_source/Limits.md#capacity-units-and-provisioned-throughput.md#provisioned-throughput.
writeCapacityUnits int64
WriteCapacityUnits int64
// If the table doesn't exist yet, gokv creates it.
// If WaitForTableCreation is true, gokv will block until the table is created, with a timeout of 15 seconds.
// If the table still doesn't exist after 15 seconds, an error is returned.
@@ -53,15 +60,23 @@ type Config struct {
// In the latter case you need to make sure that you don't read from or write to the table before it's created,
// because otherwise you will get ResourceNotFoundException errors.
// Optional (true by default).
waitForTableCreation *bool
WaitForTableCreation *bool
}
type Credentials struct {
AccessKey string
SecretAccessKey string
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Table: "fiber_storage",
readCapacityUnits: 5,
writeCapacityUnits: 5,
waitForTableCreation: aws.Bool(true),
Credentials: Credentials{},
MaxAttempts: 3,
Reset: false,
ReadCapacityUnits: 5,
WriteCapacityUnits: 5,
WaitForTableCreation: aws.Bool(true),
}
// configDefault is a helper function to set default values
@@ -78,5 +93,18 @@ func configDefault(config ...Config) Config {
if cfg.Table == "" {
cfg.Table = ConfigDefault.Table
}
if cfg.MaxAttempts == 0 {
cfg.MaxAttempts = ConfigDefault.MaxAttempts
}
if cfg.ReadCapacityUnits == 0 {
cfg.ReadCapacityUnits = ConfigDefault.ReadCapacityUnits
}
if cfg.WriteCapacityUnits == 0 {
cfg.WriteCapacityUnits = ConfigDefault.WriteCapacityUnits
}
if cfg.WaitForTableCreation == nil {
cfg.WaitForTableCreation = ConfigDefault.WaitForTableCreation
}
return cfg
}

View File

@@ -3,90 +3,23 @@ package dynamodb
import (
"context"
"errors"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
awsdynamodb "github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
awsdynamodb "github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// Storage interface that is implemented by storage providers
type Storage struct {
db *awsdynamodb.DynamoDB
table string
}
// New creates a new storage
func New(config Config) *Storage {
// Set default config
cfg := configDefault(config)
// Create db
var creds *credentials.Credentials
if (cfg.AWSaccessKeyID != "" && cfg.AWSsecretAccessKey == "") || (cfg.AWSaccessKeyID == "" && cfg.AWSsecretAccessKey != "") {
panic("[DynamoDB] You need to set BOTH AWSaccessKeyID AND AWSsecretAccessKey")
} else if cfg.AWSaccessKeyID != "" {
// Due to the previous check we can be sure that in this case AWSsecretAccessKey is not empty as well.
creds = credentials.NewStaticCredentials(cfg.AWSaccessKeyID, cfg.AWSsecretAccessKey, "")
}
// Set database options
opt := aws.NewConfig()
if cfg.Region != "" {
opt = opt.WithRegion(cfg.Region)
}
if creds != nil {
opt = opt.WithCredentials(creds)
}
if cfg.CustomEndpoint != "" {
opt = opt.WithEndpoint(cfg.CustomEndpoint)
}
sessionOpt := session.Options{
SharedConfigState: session.SharedConfigEnable,
}
// ...but allow overwrite of region and credentials if they are set in the options.
sessionOpt.Config.MergeIn(opt)
session, err := session.NewSessionWithOptions(sessionOpt)
if err != nil {
panic(err)
}
svc := awsdynamodb.New(session)
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
describeTableInput := awsdynamodb.DescribeTableInput{
TableName: &cfg.Table,
}
_, err = svc.DescribeTableWithContext(timeoutCtx, &describeTableInput)
if err != nil {
awsErr, ok := err.(awserr.Error)
if !ok {
panic(err)
} else if awsErr.Code() == awsdynamodb.ErrCodeResourceNotFoundException {
err = createTable(cfg.Table, cfg.readCapacityUnits, cfg.writeCapacityUnits, *cfg.waitForTableCreation, describeTableInput, svc)
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
// Create storage
store := &Storage{
db: svc,
table: cfg.Table,
}
// Start garbage collector
//go store.gc()
return store
db *awsdynamodb.Client
table string
requestTimeout time.Duration
}
// "k" is used as table column name for the key.
@@ -95,126 +28,182 @@ var keyAttrName = "k"
// "v" is used as table column name for the value.
var valAttrName = "v"
type table struct {
K string
V []byte
}
// New creates a new storage
func New(config Config) *Storage {
// Set default config
cfg := configDefault(config)
awscfg, err := returnAWSConfig(cfg)
if err != nil {
panic(fmt.Sprintf("unable to load SDK config, %v", err))
}
// Create db
sess := awsdynamodb.NewFromConfig(awscfg)
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
describeTableInput := awsdynamodb.DescribeTableInput{
TableName: &cfg.Table,
}
// Create storage
store := &Storage{
db: sess,
table: cfg.Table,
}
// Create table
_, err = sess.DescribeTable(timeoutCtx, &describeTableInput)
if err != nil {
var rnfe *types.ResourceNotFoundException
if errors.As(err, &rnfe) {
err := store.createTable(cfg, describeTableInput)
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
return store
}
// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
k := make(map[string]*awsdynamodb.AttributeValue)
k[keyAttrName] = &awsdynamodb.AttributeValue{
S: &key,
ctx, cancel := s.requestContext()
defer cancel()
k := make(map[string]types.AttributeValue)
k[keyAttrName] = &types.AttributeValueMemberS{
Value: key,
}
getItemInput := awsdynamodb.GetItemInput{
TableName: &s.table,
Key: k,
}
getItemOutput, err := s.db.GetItem(&getItemInput)
getItemOutput, err := s.db.GetItem(ctx, &getItemInput)
if err != nil {
var rnfe *types.ResourceNotFoundException
if errors.As(err, &rnfe) {
return nil, nil
}
return nil, err
} else if getItemOutput.Item == nil {
return nil, nil
}
attributeVal := getItemOutput.Item[valAttrName]
if attributeVal == nil {
return nil, nil
}
return attributeVal.B, nil
item := &table{}
err = attributevalue.UnmarshalMap(getItemOutput.Item, &item)
return item.V, err
}
// Set key with value
// Set key with value
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
ctx, cancel := s.requestContext()
defer cancel()
// Ain't Nobody Got Time For That
if len(key) <= 0 || len(val) <= 0 {
return nil
}
item := make(map[string]*awsdynamodb.AttributeValue)
item[keyAttrName] = &awsdynamodb.AttributeValue{
S: &key,
item := make(map[string]types.AttributeValue)
item[keyAttrName] = &types.AttributeValueMemberS{
Value: key,
}
item[valAttrName] = &awsdynamodb.AttributeValue{
B: val,
item[valAttrName] = &types.AttributeValueMemberB{
Value: val,
}
putItemInput := awsdynamodb.PutItemInput{
TableName: &s.table,
Item: item,
}
_, err := s.db.PutItem(&putItemInput)
_, err := s.db.PutItem(ctx, &putItemInput)
return err
}
// Delete entry by key
func (s *Storage) Delete(key string) error {
ctx, cancel := s.requestContext()
defer cancel()
// Ain't Nobody Got Time For That
if len(key) <= 0 {
return nil
}
k := make(map[string]*awsdynamodb.AttributeValue)
k[keyAttrName] = &awsdynamodb.AttributeValue{
S: &key,
k := make(map[string]types.AttributeValue)
k[keyAttrName] = &types.AttributeValueMemberS{
Value: key,
}
deleteItemInput := awsdynamodb.DeleteItemInput{
TableName: &s.table,
Key: k,
}
_, err := s.db.DeleteItem(&deleteItemInput)
_, err := s.db.DeleteItem(ctx, &deleteItemInput)
return err
}
// Reset all entries, including unexpired
func (s *Storage) Reset() error {
ctx, cancel := s.requestContext()
defer cancel()
deleteTableInput := awsdynamodb.DeleteTableInput{
TableName: &s.table,
}
_, err := s.db.DeleteTable(&deleteTableInput)
_, err := s.db.DeleteTable(ctx, &deleteTableInput)
return err
}
// Close the database
func (s *Storage) Close() error {
// In the DynamoDB implementation this doesn't have any effect.
return nil
}
// GC deletes all expired entries
// func (s *Storage) gc() {
// ticker := time.NewTicker(s.gcInterval)
// defer ticker.Stop()
// for {
// select {
// case <-s.done:
// return
// case t := <-ticker.C:
// _, _ = s.db.Exec(s.sqlGC, t.Unix())
// }
// }
// }
func (s *Storage) createTable(cfg Config, describeTableInput awsdynamodb.DescribeTableInput) error {
ctx, cancel := s.requestContext()
defer cancel()
func createTable(tableName string, readCapacityUnits, writeCapacityUnits int64, waitForTableCreation bool, describeTableInput awsdynamodb.DescribeTableInput, svc *awsdynamodb.DynamoDB) error {
keyAttrType := "S" // For "string"
keyType := "HASH" // As opposed to "RANGE"
createTableInput := awsdynamodb.CreateTableInput{
TableName: &tableName,
AttributeDefinitions: []*awsdynamodb.AttributeDefinition{{
TableName: &s.table,
AttributeDefinitions: []types.AttributeDefinition{{
AttributeName: &keyAttrName,
AttributeType: &keyAttrType,
AttributeType: types.ScalarAttributeType(keyAttrType),
}},
KeySchema: []*awsdynamodb.KeySchemaElement{{
KeySchema: []types.KeySchemaElement{{
AttributeName: &keyAttrName,
KeyType: &keyType,
KeyType: types.KeyType(keyType),
}},
ProvisionedThroughput: &awsdynamodb.ProvisionedThroughput{
ReadCapacityUnits: &readCapacityUnits,
WriteCapacityUnits: &writeCapacityUnits,
ProvisionedThroughput: &types.ProvisionedThroughput{
ReadCapacityUnits: &cfg.ReadCapacityUnits,
WriteCapacityUnits: &cfg.WriteCapacityUnits,
},
}
_, err := svc.CreateTable(&createTableInput)
_, err := s.db.CreateTable(ctx, &createTableInput)
if err != nil {
return err
}
// If configured (true by default), block until the table is created.
// Typical table creation duration is 10 seconds.
if waitForTableCreation {
if *cfg.WaitForTableCreation {
for try := 1; try < 16; try++ {
describeTableOutput, err := svc.DescribeTable(&describeTableInput)
if err != nil || *describeTableOutput.Table.TableStatus == "CREATING" {
describeTableOutput, err := s.db.DescribeTable(ctx, &describeTableInput)
if err != nil || describeTableOutput.Table.TableStatus == "CREATING" {
time.Sleep(1 * time.Second)
} else {
break
@@ -222,14 +211,56 @@ func createTable(tableName string, readCapacityUnits, writeCapacityUnits int64,
}
// Last try (16th) after 15 seconds of waiting.
// Now handle error as such.
describeTableOutput, err := svc.DescribeTable(&describeTableInput)
describeTableOutput, err := s.db.DescribeTable(ctx, &describeTableInput)
if err != nil {
return errors.New("The DynamoDB table couldn't be created")
return errors.New("dynamodb: the table couldn't be created")
}
if *describeTableOutput.Table.TableStatus == "CREATING" {
return errors.New("The DynamoDB table took too long to be created")
if describeTableOutput.Table.TableStatus == "CREATING" {
return errors.New("dynamodb: the table took too long to be created")
}
}
return nil
}
// Context for making requests will timeout if a non-zero timeout is configured
func (s *Storage) requestContext() (context.Context, context.CancelFunc) {
if s.requestTimeout > 0 {
return context.WithTimeout(context.Background(), s.requestTimeout)
}
return context.Background(), func() {}
}
func returnAWSConfig(cfg Config) (aws.Config, error) {
endpoint := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if cfg.Endpoint != "" {
return aws.Endpoint{
PartitionID: "aws",
URL: cfg.Endpoint,
SigningRegion: cfg.Region,
HostnameImmutable: true,
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})
if cfg.Credentials != (Credentials{}) {
credentials := credentials.NewStaticCredentialsProvider(cfg.Credentials.AccessKey, cfg.Credentials.SecretAccessKey, "")
return awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion(cfg.Region),
awsconfig.WithEndpointResolverWithOptions(endpoint),
awsconfig.WithCredentialsProvider(credentials),
awsconfig.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
}),
)
}
return awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion(cfg.Region),
awsconfig.WithEndpointResolverWithOptions(endpoint),
awsconfig.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
}),
)
}

View File

@@ -1 +1,107 @@
package dynamodb
import (
"testing"
"github.com/gofiber/utils"
)
var testStore = New(
Config{
Table: "fiber_storage",
Endpoint: "http://localhost:8000/",
Region: "us-east-1",
Credentials: Credentials{
AccessKey: "dummy",
SecretAccessKey: "dummy",
},
},
)
func Test_DynamoDB_Set(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
}
func Test_DynamoDB_Set_Override(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
err = testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
}
func Test_DynamoDB_Get(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, val, result)
}
func Test_DynamoDB_Get_NotExist(t *testing.T) {
result, err := testStore.Get("notexist")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(result) == 0)
}
func Test_DynamoDB_Delete(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
err = testStore.Delete(key)
utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(result) == 0)
}
func Test_DynamoDB_Reset(t *testing.T) {
var (
val = []byte("doe")
)
err := testStore.Set("john1", val, 0)
utils.AssertEqual(t, nil, err)
err = testStore.Set("john2", val, 0)
utils.AssertEqual(t, nil, err)
err = testStore.Reset()
utils.AssertEqual(t, nil, err)
result, err := testStore.Get("john1")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(result) == 0)
result, err = testStore.Get("john2")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(result) == 0)
}
func Test_DynamoDB_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close())
}

View File

@@ -2,4 +2,11 @@ module github.com/gofiber/storage/dynamodb
go 1.14
require github.com/aws/aws-sdk-go v1.42.38
require (
github.com/aws/aws-sdk-go-v2 v1.15.0
github.com/aws/aws-sdk-go-v2/config v1.15.0
github.com/aws/aws-sdk-go-v2/credentials v1.10.0
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.8.0
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.15.0
github.com/gofiber/utils v0.1.2
)

View File

@@ -1,23 +1,50 @@
github.com/aws/aws-sdk-go v1.42.38 h1:/fNQTB4ZUQOa8+cfX7C7F0zyXRdiN1jGKKXt3+5nmzM=
github.com/aws/aws-sdk-go v1.42.38/go.mod h1:OGr6lGMAKGlG9CVrYnWYDKIyb829c6EVBRjxqjmPepc=
github.com/aws/aws-sdk-go-v2 v1.15.0 h1:f9kWLNfyCzCB43eupDAk3/XgJ2EpgktiySD6leqs0js=
github.com/aws/aws-sdk-go-v2 v1.15.0/go.mod h1:lJYcuZZEHWNIb6ugJjbQY1fykdoobWbOS7kJYb4APoI=
github.com/aws/aws-sdk-go-v2/config v1.15.0 h1:cibCYF2c2uq0lsbu0Ggbg8RuGeiHCmXwUlTMS77CiK4=
github.com/aws/aws-sdk-go-v2/config v1.15.0/go.mod h1:NccaLq2Z9doMmeQXHQRrt2rm+2FbkrcPvfdbCaQn5hY=
github.com/aws/aws-sdk-go-v2/credentials v1.10.0 h1:M/FFpf2w31F7xqJqJLgiM0mFpLOtBvwZggORr6QCpo8=
github.com/aws/aws-sdk-go-v2/credentials v1.10.0/go.mod h1:HWJMr4ut5X+Lt/7epc7I6Llg5QIcoFHKAeIzw32t6EE=
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.8.0 h1:XxTy21xVUkoCZOSGwf+AW22v8aK3eEbYMaGGQ3MbKKk=
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.8.0/go.mod h1:6WkjzWenkrj3IgLPIPBBz4Qh99jNDF8L4Wj03vfMhAA=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.0 h1:gUlb+I7NwDtqJUIRcFYDiheYa97PdVHG/5Iz+SwdoHE=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.0/go.mod h1:prX26x9rmLwkEE1VVCelQOQgRN9sOVIssgowIJ270SE=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.6 h1:xiGjGVQsem2cxoIX61uRGy+Jux2s9C/kKbTrWLdrU54=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.6/go.mod h1:SSPEdf9spsFgJyhjrXvawfpyzrXHBCUe+2eQ1CjC1Ak=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.0 h1:bt3zw79tm209glISdMRCIVRCwvSDXxgAxh5KWe2qHkY=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.0/go.mod h1:viTrxhAuejD+LszDahzAE2x40YjYWhMqzHxv2ZiWaME=
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.7 h1:QOMEP8jnO8sm0SX/4G7dbaIq2eEP2wcWEsF0jzrXLJc=
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.7/go.mod h1:P5sjYYf2nc5dE6cZIzEMsVtq6XeLD7c4rM+kQJPrByA=
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.15.0 h1:qnx+WyIH9/AD+wAxi05WCMNanO236ceqHg6hChCWs3M=
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.15.0/go.mod h1:+Kc1UmbE37ijaAsb3KogW6FR8z0myjX6VtdcCkQEK0k=
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.13.0 h1:s71pGCiLqqGRoUWtdJ2j4PazwEpZVwQc16na/4FfXdk=
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.13.0/go.mod h1:YGzTq/joAih4HRZZtMBWGP4bI8xVucOBQ9RvuanpclA=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.0 h1:uhb7moM7VjqIEpWzTpCvceLDSwrWpaleXm39OnVjuLE=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.0/go.mod h1:pA2St3Pu2Ldy6fBPY45Azoh1WBG4oS7eIKOd4XN7Meg=
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.7.0 h1:6Bc0KHhAyxGe15JUHrK+Udw7KhE5LN+5HKZjQGo4yDI=
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.7.0/go.mod h1:0nXuX9UrkN4r0PX9TSKfcueGRfsdEYIKG4rjTeJ61X8=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.0 h1:YQ3fTXACo7xeAqg0NiqcCmBOXJruUfh+4+O2qxF2EjQ=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.0/go.mod h1:R31ot6BgESRCIoxwfKtIHzZMo/vsZn2un81g9BJ4nmo=
github.com/aws/aws-sdk-go-v2/service/sso v1.11.0 h1:gZLEXLH6NiU8Y52nRhK1jA+9oz7LZzBK242fi/ziXa4=
github.com/aws/aws-sdk-go-v2/service/sso v1.11.0/go.mod h1:d1WcT0OjggjQCAdOkph8ijkr5sUwk1IH/VenOn7W1PU=
github.com/aws/aws-sdk-go-v2/service/sts v1.16.0 h1:0+X/rJ2+DTBKWbUsn7WtF0JvNk/fRf928vkFsXkbbZs=
github.com/aws/aws-sdk-go-v2/service/sts v1.16.0/go.mod h1:+8k4H2ASUZZXmjx/s3DFLo9tGBb44lkz3XcgfypJY7s=
github.com/aws/smithy-go v1.11.1 h1:IQ+lPZVkSM3FRtyaDox41R8YS6iwPMYIreejOgPW49g=
github.com/aws/smithy-go v1.11.1/go.mod h1:3xHYmszWVx2c0kIwQeEVf9uSm4fYZt67FBJnwub1bgM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f h1:hEYJvxw1lSnWIl8X9ofsYMklzaDs90JI2az5YMd4fPM=
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=

View File

@@ -7,7 +7,7 @@ require (
github.com/gofiber/utils v0.1.2
github.com/golang/snappy v0.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.mongodb.org/mongo-driver v1.8.2
go.mongodb.org/mongo-driver v1.8.4
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/text v0.3.7 // indirect

View File

@@ -37,8 +37,8 @@ github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk=
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4=
go.mongodb.org/mongo-driver v1.8.2 h1:8ssUXufb90ujcIvR6MyE1SchaNj0SFxsakiZgxIyrMk=
go.mongodb.org/mongo-driver v1.8.2/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY=
go.mongodb.org/mongo-driver v1.8.4 h1:NruvZPPL0PBcRJKmbswoWSrmHeUvzdxA3GCPfD/NEOA=
go.mongodb.org/mongo-driver v1.8.4/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=

View File

@@ -42,6 +42,9 @@ func Test_Ristretto_Get(t *testing.T) {
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
// stabilize with some delay in between -> bug already communicated
time.Sleep(10000)
result, err := testStore.Get(key)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, val, result)
@@ -86,6 +89,9 @@ func Test_Ristretto_Delete(t *testing.T) {
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
// stabilize with some delay in between -> bug already communicated
time.Sleep(10000)
err = testStore.Delete(key)
utils.AssertEqual(t, nil, err)

108
s3/README.md Normal file
View File

@@ -0,0 +1,108 @@
# S3
A S3 storage driver using [aws/aws-sdk-go-v2](https://github.com/aws/aws-sdk-go-v2).
**Note:** If config fields of credentials not given, credentials are using from the environment variables, ~/.aws/credentials, or EC2 instance role. If config fields of credentials given, credentials are using from config. Look at: [specifying credentials](https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials)
### Table of Contents
- [Signatures](#signatures)
- [Installation](#installation)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)
### Signatures
```go
func New(config ...Config) Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
```
### Installation
S3 is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
```bash
go mod init github.com/<user>/<repo>
```
And then install the s3 implementation:
```bash
go get github.com/gofiber/storage/s3
```
### Examples
Import the storage package.
```go
import "github.com/gofiber/storage/s3"
```
You can use the following possibilities to create a storage:
```go
// Initialize default config
store := s3.New()
// Initialize custom config
store := s3.New(s3.Config{
Bucket: "my-bucket-url",
Endpoint: "my-endpoint",
Region: "my-region",
Reset: false,
})
```
### Config
```go
// Config defines the config for storage.
type Config struct {
// S3 bucket name
Bucket string
// AWS endpoint
Endpoint string
// AWS region
Region string
// Request timeout
//
// Optional. Default is 0 (no timeout)
RequestTimeout time.Duration
// Reset clears any existing keys in existing Bucket
//
// Optional. Default is false
Reset bool
// Credentials overrides AWS access key and AWS secret access key. Not recommended.
//
// Optional. Default is Credentials{}
Credentials Credentials
// The maximum number of times requests that encounter retryable failures should be attempted.
//
// Optional. Default is 3
MaxAttempts int
}
type Credentials struct {
AccessKey string
SecretAccessKey string
}
```
### Default Config
The default configuration lacks Bucket, Region, and Endpoint which are all required and must be overwritten:
```go
// ConfigDefault is the default config
var ConfigDefault = Config{
Bucket: "",
Region: "",
Endpoint: "",
Credentials: Credentials{},
MaxAttempts: 3,
RequestTimeout: 0,
Reset: false,
}
```

69
s3/config.go Normal file
View File

@@ -0,0 +1,69 @@
package s3
import "time"
// Config defines the config for storage.
type Config struct {
// S3 bucket name
Bucket string
// AWS endpoint
Endpoint string
// AWS region
Region string
// Request timeout
//
// Optional. Default is 0 (no timeout)
RequestTimeout time.Duration
// Reset clears any existing keys in existing Bucket
//
// Optional. Default is false
Reset bool
// Credentials overrides AWS access key and AWS secret access key. Not recommended.
//
// Optional. Default is Credentials{}
Credentials Credentials
// The maximum number of times requests that encounter retryable failures should be attempted.
//
// Optional. Default is 3
MaxAttempts int
}
type Credentials struct {
AccessKey string
SecretAccessKey string
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Bucket: "",
Region: "",
Endpoint: "",
Credentials: Credentials{},
MaxAttempts: 3,
RequestTimeout: 0,
Reset: false,
}
// Helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}
// Override default config
cfg := config[0]
// Set default values
if cfg.Bucket == "" {
cfg.Bucket = ConfigDefault.Bucket
}
return cfg
}

12
s3/go.mod Normal file
View File

@@ -0,0 +1,12 @@
module github.com/gofiber/storage/s3
go 1.16
require (
github.com/aws/aws-sdk-go-v2 v1.13.0
github.com/aws/aws-sdk-go-v2/config v1.13.1
github.com/aws/aws-sdk-go-v2/credentials v1.8.0
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.9.1
github.com/aws/aws-sdk-go-v2/service/s3 v1.24.1
github.com/gofiber/utils v0.1.2
)

51
s3/go.sum Normal file
View File

@@ -0,0 +1,51 @@
github.com/aws/aws-sdk-go-v2 v1.13.0 h1:1XIXAfxsEmbhbj5ry3D3vX+6ZcUYvIqSm4CWWEuGZCA=
github.com/aws/aws-sdk-go-v2 v1.13.0/go.mod h1:L6+ZpqHaLbAaxsqV0L4cvxZY7QupWJB4fhkf8LXvC7w=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.2.0 h1:scBthy70MB3m4LCMFaBcmYCyR2XWOz6MxSfdSu/+fQo=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.2.0/go.mod h1:oZHzg1OVbuCiRTY0oRPM+c2HQvwnFCGJwKeSqqAJ/yM=
github.com/aws/aws-sdk-go-v2/config v1.13.1 h1:yLv8bfNoT4r+UvUKQKqRtdnvuWGMK5a82l4ru9Jvnuo=
github.com/aws/aws-sdk-go-v2/config v1.13.1/go.mod h1:Ba5Z4yL/UGbjQUzsiaN378YobhFo0MLfueXGiOsYtEs=
github.com/aws/aws-sdk-go-v2/credentials v1.8.0 h1:8Ow0WcyDesGNL0No11jcgb1JAtE+WtubqXjgxau+S0o=
github.com/aws/aws-sdk-go-v2/credentials v1.8.0/go.mod h1:gnMo58Vwx3Mu7hj1wpcG8DI0s57c9o42UQ6wgTQT5to=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.10.0 h1:NITDuUZO34mqtOwFWZiXo7yAHj7kf+XPE+EiKuCBNUI=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.10.0/go.mod h1:I6/fHT/fH460v09eg2gVrd8B/IqskhNdpcLH0WNO3QI=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.9.1 h1:oUCLhAKNaXyTqdJyw+KEjDVVBs1V5mCy8YDLMi08LL8=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.9.1/go.mod h1:pB38jI+AdaPoLAgaL9bwxDdy6rjwO6LIArBZDLjq6zs=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.4 h1:CRiQJ4E2RhfDdqbie1ZYDo8QtIo75Mk7oTdJSfwJTMQ=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.4/go.mod h1:XHgQ7Hz2WY2GAn//UXHofLfPXWh+s62MbMOijrg12Lw=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.2.0 h1:3ADoioDMOtF4uiK59vCpplpCwugEU+v4ZFD29jDL3RQ=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.2.0/go.mod h1:BsCSJHx5DnDXIrOcqB8KN1/B+hXLG/bi4Y6Vjcx/x9E=
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.5 h1:ixotxbfTCFpqbuwFv/RcZwyzhkxPSYDYEMcj4niB5Uk=
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.5/go.mod h1:R3sWUqPcfXSiF/LSFJhjyJmpg9uV6yP2yv3YZZjldVI=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.7.0 h1:F1diQIOkNn8jcez4173r+PLPdkWK7chy74r3fKpDrLI=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.7.0/go.mod h1:8ctElVINyp+SjhoZZceUAZw78glZH6R8ox5MVNu5j2s=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.7.0 h1:4QAOB3KrvI1ApJK14sliGr3Ie2pjyvNypn/lfzDHfUw=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.7.0/go.mod h1:K/qPe6AP2TGYv4l6n7c88zh9jWBDf6nHhvg1fx/EWfU=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.11.0 h1:XAe+PDnaBELHr25qaJKfB415V4CKFWE8H+prUreql8k=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.11.0/go.mod h1:RMlgnt1LbOT2BxJ3cdw+qVz7KL84714LFkWtF6sLI7A=
github.com/aws/aws-sdk-go-v2/service/s3 v1.24.1 h1:zAU2P99CLTz8kUGl+IptU2ycAXuMaLAvgIv+UH4U8pY=
github.com/aws/aws-sdk-go-v2/service/s3 v1.24.1/go.mod h1:oIUXg/5F0x0gy6nkwEnlxZboueddwPEKO6Xl+U6/3a0=
github.com/aws/aws-sdk-go-v2/service/sso v1.9.0 h1:1qLJeQGBmNQW3mBNzK2CFmrQNmoXWrscPqsrAaU1aTA=
github.com/aws/aws-sdk-go-v2/service/sso v1.9.0/go.mod h1:vCV4glupK3tR7pw7ks7Y4jYRL86VvxS+g5qk04YeWrU=
github.com/aws/aws-sdk-go-v2/service/sts v1.14.0 h1:ksiDXhvNYg0D2/UFkLejsaz3LqpW5yjNQ8Nx9Sn2c0E=
github.com/aws/aws-sdk-go-v2/service/sts v1.14.0/go.mod h1:u0xMJKDvvfocRjiozsoZglVNXRG19043xzp3r2ivLIk=
github.com/aws/smithy-go v1.10.0 h1:gsoZQMNHnX+PaghNw4ynPsyGP7aUCqx5sY2dlPQsZ0w=
github.com/aws/smithy-go v1.10.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

202
s3/s3.go Normal file
View File

@@ -0,0 +1,202 @@
package s3
import (
"bytes"
"context"
"errors"
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
)
// Storage interface that is implemented by storage providers
type Storage struct {
svc *s3.Client
downloader *manager.Downloader
uploader *manager.Uploader
requestTimeout time.Duration
bucket string
}
// New creates a new storage
func New(config ...Config) *Storage {
// Set default config
cfg := configDefault(config...)
// Create s3 session
// If config fields of credentials not given, credentials are using from the environment variables, ~/.aws/credentials, or EC2 instance role.
// If config fields of credentials given, credentials are using from config.
//
// Look at: https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials
awscfg, err := returnAWSConfig(cfg)
if err != nil {
panic(fmt.Sprintf("unable to load SDK config, %v", err))
}
sess := s3.NewFromConfig(awscfg)
storage := &Storage{
svc: sess,
downloader: manager.NewDownloader(sess),
uploader: manager.NewUploader(sess),
requestTimeout: cfg.RequestTimeout,
bucket: cfg.Bucket,
}
// Reset all entries if set to true
if cfg.Reset {
if err := storage.Reset(); err != nil {
panic(err)
}
}
return storage
}
// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
var nsk *types.NoSuchKey
if len(key) <= 0 {
return nil, nil
}
ctx, cancel := s.requestContext()
defer cancel()
buf := manager.NewWriteAtBuffer([]byte{})
_, err := s.downloader.Download(ctx, buf, &s3.GetObjectInput{
Bucket: &s.bucket,
Key: aws.String(key),
})
if errors.As(err, &nsk) {
return nil, nil
}
return buf.Bytes(), err
}
// Set key with value
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
if len(key) <= 0 {
return nil
}
ctx, cancel := s.requestContext()
defer cancel()
_, err := s.uploader.Upload(ctx, &s3.PutObjectInput{
Bucket: &s.bucket,
Key: aws.String(key),
Body: bytes.NewReader(val),
})
return err
}
// Delete entry by key
func (s *Storage) Delete(key string) error {
if len(key) <= 0 {
return nil
}
ctx, cancel := s.requestContext()
defer cancel()
_, err := s.svc.DeleteObject(ctx, &s3.DeleteObjectInput{
Bucket: &s.bucket,
Key: aws.String(key),
})
return err
}
// Reset all entries, including unexpired
func (s *Storage) Reset() error {
ctx, cancel := s.requestContext()
defer cancel()
paginator := s3.NewListObjectsV2Paginator(s.svc, &s3.ListObjectsV2Input{
Bucket: &s.bucket,
})
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
return err
}
var objects []types.ObjectIdentifier
for _, object := range page.Contents {
objects = append(objects, types.ObjectIdentifier{
Key: object.Key,
})
}
_, err = s.svc.DeleteObjects(ctx, &s3.DeleteObjectsInput{
Bucket: &s.bucket,
Delete: &types.Delete{
Objects: objects,
},
})
if err != nil {
return err
}
}
return nil
}
// Close the database
func (s *Storage) Close() error {
return nil
}
// Context for making requests will timeout if a non-zero timeout is configured
func (s *Storage) requestContext() (context.Context, context.CancelFunc) {
if s.requestTimeout > 0 {
return context.WithTimeout(context.Background(), s.requestTimeout)
}
return context.Background(), func() {}
}
func returnAWSConfig(cfg Config) (aws.Config, error) {
endpoint := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if cfg.Endpoint != "" {
return aws.Endpoint{
PartitionID: "aws",
URL: cfg.Endpoint,
SigningRegion: cfg.Region,
HostnameImmutable: true,
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})
if cfg.Credentials != (Credentials{}) {
credentials := credentials.NewStaticCredentialsProvider(cfg.Credentials.AccessKey, cfg.Credentials.SecretAccessKey, "")
return awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion(cfg.Region),
awsconfig.WithEndpointResolverWithOptions(endpoint),
awsconfig.WithCredentialsProvider(credentials),
awsconfig.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
}),
)
}
return awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion(cfg.Region),
awsconfig.WithEndpointResolverWithOptions(endpoint),
awsconfig.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
}),
)
}

107
s3/s3_test.go Normal file
View File

@@ -0,0 +1,107 @@
package s3
import (
"testing"
"github.com/gofiber/utils"
)
var testStore = New(
Config{
Bucket: "testbucket",
Endpoint: "http://127.0.0.1:9000/",
Region: "us-east-1",
Credentials: Credentials{
AccessKey: "minioadmin",
SecretAccessKey: "minioadmin",
},
},
)
func Test_S3_Set(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
}
func Test_S3_Set_Override(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
err = testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
}
func Test_S3_Get(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, val, result)
}
func Test_S3_Get_NotExist(t *testing.T) {
result, err := testStore.Get("notexist")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(result) == 0)
}
func Test_S3_Delete(t *testing.T) {
var (
key = "john"
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
err = testStore.Delete(key)
utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(result) == 0)
}
func Test_S3_Reset(t *testing.T) {
var (
val = []byte("doe")
)
err := testStore.Set("john1", val, 0)
utils.AssertEqual(t, nil, err)
err = testStore.Set("john2", val, 0)
utils.AssertEqual(t, nil, err)
err = testStore.Reset()
utils.AssertEqual(t, nil, err)
result, err := testStore.Get("john1")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(result) == 0)
result, err = testStore.Get("john2")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, len(result) == 0)
}
func Test_S3_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close())
}

View File

@@ -4,5 +4,5 @@ go 1.14
require (
github.com/gofiber/utils v0.1.2
github.com/mattn/go-sqlite3 v1.14.10
github.com/mattn/go-sqlite3 v1.14.12
)

View File

@@ -1,4 +1,4 @@
github.com/gofiber/utils v0.1.2 h1:1SH2YEz4RlNS0tJlMJ0bGwO0JkqPqvq6TbHK9tXZKtk=
github.com/gofiber/utils v0.1.2/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
github.com/mattn/go-sqlite3 v1.14.10 h1:MLn+5bFRlWMGoSRmJour3CL1w/qL96mvipqpwQW/Sfk=
github.com/mattn/go-sqlite3 v1.14.10/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0=
github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=