diff --git a/s3/README.md b/s3/README.md index 95bf6291..4134fcbb 100644 --- a/s3/README.md +++ b/s3/README.md @@ -32,7 +32,9 @@ func (s *Storage) Reset() error func (s *Storage) Close() error func (s *Storage) Conn() *s3.Client ``` + ### 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// @@ -43,7 +45,9 @@ go get github.com/gofiber/storage/s3/v2 ``` ### Examples + Import the storage package. + ```go 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 ```go // Config defines the config for storage. diff --git a/s3/s3_methods.go b/s3/s3_methods.go index 4372b95f..6307ae4a 100644 --- a/s3/s3_methods.go +++ b/s3/s3_methods.go @@ -2,6 +2,7 @@ package s3 import ( "bytes" + "fmt" "github.com/aws/aws-sdk-go-v2/aws" "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. // SetWithChecksum sets key with value and checksum. -// Key of `checksum` map is algorithm in upper cases like `CRC32`, `CRC32C`, -// `SHA1`, `SHA256`, value is the checksum. -func (s *Storage) SetWithChecksum(key string, val []byte, checksum map[string]string) error { +// Key of `checksum` map is algorithm in upper cases, value is the checksum. +// 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). +func (s *Storage) SetWithChecksum(key string, val []byte, checksum map[string][]byte) error { if len(key) <= 0 { return nil } @@ -29,13 +32,15 @@ func (s *Storage) SetWithChecksum(key string, val []byte, checksum map[string]st for alg, sum := range checksum { switch alg { case "CRC32": - poi.ChecksumCRC32 = aws.String(sum) + poi.ChecksumCRC32 = aws.String(fmt.Sprintf("%x", sum)) case "CRC32C": - poi.ChecksumCRC32C = aws.String(sum) + poi.ChecksumCRC32C = aws.String(fmt.Sprintf("%x", sum)) case "SHA1": - poi.ChecksumSHA1 = aws.String(sum) + poi.ChecksumSHA1 = aws.String(fmt.Sprintf("%x", sum)) case "SHA256": - poi.ChecksumSHA256 = aws.String(sum) + poi.ChecksumSHA256 = aws.String(fmt.Sprintf("%x", sum)) + default: + return fmt.Errorf("invalid checksum algorithm: %s", alg) } } diff --git a/s3/s3_methods_test.go b/s3/s3_methods_test.go index 866acea1..0a390225 100644 --- a/s3/s3_methods_test.go +++ b/s3/s3_methods_test.go @@ -14,7 +14,7 @@ func Test_S3_SetWithChecksum(t *testing.T) { 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) result, err := testStore.Get(key)