Add example in README.

Use `map[string][]byte` instead of `map[string]string`.
This commit is contained in:
Zhang Huangbin
2023-09-05 16:26:55 +08:00
parent db6c457d5c
commit 1ddcc7b3fc
3 changed files with 36 additions and 8 deletions

View File

@@ -32,7 +32,9 @@ func (s *Storage) Reset() error
func (s *Storage) Close() error func (s *Storage) Close() error
func (s *Storage) Conn() *s3.Client func (s *Storage) Conn() *s3.Client
``` ```
### Installation ### 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: 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 ```bash
go mod init github.com/<user>/<repo> go mod init github.com/<user>/<repo>
@@ -43,7 +45,9 @@ go get github.com/gofiber/storage/s3/v2
``` ```
### Examples ### Examples
Import the storage package. Import the storage package.
```go ```go
import "github.com/gofiber/storage/s3/v2" import "github.com/gofiber/storage/s3/v2"
``` ```
@@ -62,6 +66,25 @@ store := s3.New(s3.Config{
}) })
``` ```
Create an object with `Set()`:
```go
err := store.Set("my-key", []byte("my-value"))
```
Or, call `SetWithChecksum()` to create an object with checksum to
ask S3 server to verify data integrity on server side:
> Currently only 4 algorithm are supported: `CRC32`, `CRC32C`, `SHA1`, `SHA256`.
> For more information, see [PutObjectInput](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/s3#PutObjectInput).
```go
val := []byte("my-value")
sha256sum := sha256.New().Sum256(val)
checksum := map[string][]byte{"SHA256": sha256sum}
err := store.SetWithChecksum("my-key", []byte("my-value"), checksum)
```
### Config ### Config
```go ```go
// Config defines the config for storage. // Config defines the config for storage.

View File

@@ -2,6 +2,7 @@ package s3
import ( import (
"bytes" "bytes"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3"
@@ -10,9 +11,11 @@ import (
// Additional methods for S3, but not required by gofiber Storage interface. // Additional methods for S3, but not required by gofiber Storage interface.
// SetWithChecksum sets key with value and checksum. // SetWithChecksum sets key with value and checksum.
// Key of `checksum` map is algorithm in upper cases like `CRC32`, `CRC32C`, // Key of `checksum` map is algorithm in upper cases, value is the checksum.
// `SHA1`, `SHA256`, value is the checksum. // Currently only 4 algorithm are supported: `CRC32`, `CRC32C`, `SHA1`, `SHA256`.
func (s *Storage) SetWithChecksum(key string, val []byte, checksum map[string]string) error { //
// For more information, see [PutObjectInput](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/s3#PutObjectInput).
func (s *Storage) SetWithChecksum(key string, val []byte, checksum map[string][]byte) error {
if len(key) <= 0 { if len(key) <= 0 {
return nil return nil
} }
@@ -29,13 +32,15 @@ func (s *Storage) SetWithChecksum(key string, val []byte, checksum map[string]st
for alg, sum := range checksum { for alg, sum := range checksum {
switch alg { switch alg {
case "CRC32": case "CRC32":
poi.ChecksumCRC32 = aws.String(sum) poi.ChecksumCRC32 = aws.String(fmt.Sprintf("%x", sum))
case "CRC32C": case "CRC32C":
poi.ChecksumCRC32C = aws.String(sum) poi.ChecksumCRC32C = aws.String(fmt.Sprintf("%x", sum))
case "SHA1": case "SHA1":
poi.ChecksumSHA1 = aws.String(sum) poi.ChecksumSHA1 = aws.String(fmt.Sprintf("%x", sum))
case "SHA256": case "SHA256":
poi.ChecksumSHA256 = aws.String(sum) poi.ChecksumSHA256 = aws.String(fmt.Sprintf("%x", sum))
default:
return fmt.Errorf("invalid checksum algorithm: %s", alg)
} }
} }

View File

@@ -14,7 +14,7 @@ func Test_S3_SetWithChecksum(t *testing.T) {
sha256sum = sha256.New().Sum(val) sha256sum = sha256.New().Sum(val)
) )
err := testStore.SetWithChecksum(key, val, map[string]string{"SHA256": string(sha256sum)}) err := testStore.SetWithChecksum(key, val, map[string][]byte{"SHA256": sha256sum})
require.NoError(t, err) require.NoError(t, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)