mirror of
https://github.com/gofiber/storage.git
synced 2025-10-04 16:22:52 +08:00
Fix incorrect sum.
This commit is contained in:
13
s3/README.md
13
s3/README.md
@@ -74,13 +74,22 @@ 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`.
|
||||
> Currently 4 algorithm are supported:
|
||||
> - types.ChecksumAlgorithmCrc32 (`CRC32`)
|
||||
> - types.ChecksumAlgorithmCrc32c (`CRC32C`)
|
||||
> - types.ChecksumAlgorithmSha1 (`SHA1`)
|
||||
> - types.ChecksumAlgorithmSha256 (`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}
|
||||
|
||||
// import "github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
checksum = map[types.ChecksumAlgorithm][]byte{
|
||||
types.ChecksumAlgorithmSha256: sha256sum,
|
||||
}
|
||||
|
||||
err := store.SetWithChecksum("my-key", []byte("my-value"), checksum)
|
||||
```
|
||||
|
@@ -2,20 +2,26 @@ package s3
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"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/types"
|
||||
)
|
||||
|
||||
// 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, value is the checksum.
|
||||
// Currently only 4 algorithm are supported: `CRC32`, `CRC32C`, `SHA1`, `SHA256`.
|
||||
// Currently only 4 algorithm are supported:
|
||||
// - types.ChecksumAlgorithmCrc32 (`CRC32`)
|
||||
// - types.ChecksumAlgorithmCrc32c (`CRC32C`)
|
||||
// - types.ChecksumAlgorithmSha1 (`SHA1`)
|
||||
// - types.ChecksumAlgorithmSha256 (`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 {
|
||||
func (s *Storage) SetWithChecksum(key string, val []byte, checksum map[types.ChecksumAlgorithm][]byte) error {
|
||||
if len(key) <= 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -31,14 +37,14 @@ func (s *Storage) SetWithChecksum(key string, val []byte, checksum map[string][]
|
||||
|
||||
for alg, sum := range checksum {
|
||||
switch alg {
|
||||
case "CRC32":
|
||||
poi.ChecksumCRC32 = aws.String(fmt.Sprintf("%x", sum))
|
||||
case "CRC32C":
|
||||
poi.ChecksumCRC32C = aws.String(fmt.Sprintf("%x", sum))
|
||||
case "SHA1":
|
||||
poi.ChecksumSHA1 = aws.String(fmt.Sprintf("%x", sum))
|
||||
case "SHA256":
|
||||
poi.ChecksumSHA256 = aws.String(fmt.Sprintf("%x", sum))
|
||||
case types.ChecksumAlgorithmCrc32:
|
||||
poi.ChecksumCRC32 = aws.String(hex.EncodeToString(sum))
|
||||
case types.ChecksumAlgorithmCrc32c:
|
||||
poi.ChecksumCRC32C = aws.String(hex.EncodeToString(sum))
|
||||
case types.ChecksumAlgorithmSha1:
|
||||
poi.ChecksumSHA1 = aws.String(hex.EncodeToString(sum))
|
||||
case types.ChecksumAlgorithmSha256:
|
||||
poi.ChecksumSHA256 = aws.String(hex.EncodeToString(sum))
|
||||
default:
|
||||
return fmt.Errorf("invalid checksum algorithm: %s", alg)
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ import (
|
||||
"crypto/sha256"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -12,9 +13,12 @@ func Test_S3_SetWithChecksum(t *testing.T) {
|
||||
key = "john"
|
||||
val = []byte("doe")
|
||||
sha256sum = sha256.New().Sum(val)
|
||||
checksum = map[types.ChecksumAlgorithm][]byte{
|
||||
types.ChecksumAlgorithmSha256: sha256sum,
|
||||
}
|
||||
)
|
||||
|
||||
err := testStore.SetWithChecksum(key, val, map[string][]byte{"SHA256": sha256sum})
|
||||
err := testStore.SetWithChecksum(key, val, checksum)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := testStore.Get(key)
|
||||
|
Reference in New Issue
Block a user