mirror of
https://github.com/gofiber/storage.git
synced 2025-10-08 18:11:23 +08:00
Fix incorrect checksum (base64 encoded).
Create bucket in unittest instead of GitHub Actions.
This commit is contained in:
9
.github/workflows/test-s3.yml
vendored
9
.github/workflows/test-s3.yml
vendored
@@ -20,14 +20,7 @@ jobs:
|
|||||||
- 1.21.x
|
- 1.21.x
|
||||||
steps:
|
steps:
|
||||||
- name: Install MinIO
|
- name: Install MinIO
|
||||||
run: |
|
run: docker run -d -p 9000:9000 --name minio minio/minio server /data
|
||||||
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: Fetch Repository
|
- name: Fetch Repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Install Go
|
- name: Install Go
|
||||||
|
@@ -1,22 +1,38 @@
|
|||||||
package s3
|
package s3
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
bucket = "testbucket"
|
||||||
|
)
|
||||||
|
|
||||||
var testStore *Storage
|
var testStore *Storage
|
||||||
|
|
||||||
func init() {
|
func TestMain(m *testing.M) {
|
||||||
testStore = New(
|
testStore = New(
|
||||||
Config{
|
Config{
|
||||||
Bucket: "testbucket",
|
Bucket: bucket,
|
||||||
Endpoint: "http://127.0.0.1:9000/",
|
Endpoint: "http://127.0.0.1:9000/",
|
||||||
Region: "us-east-1",
|
Region: "us-east-1",
|
||||||
Credentials: Credentials{
|
Credentials: Credentials{
|
||||||
AccessKey: "minioadmin",
|
AccessKey: "minioadmin",
|
||||||
SecretAccessKey: "minioadmin",
|
SecretAccessKey: "minioadmin",
|
||||||
},
|
},
|
||||||
RequestTimeout: 10 * time.Second,
|
RequestTimeout: 3 * time.Second,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Create test bucket.
|
||||||
|
_ = testStore.CreateBucket(bucket)
|
||||||
|
|
||||||
|
exitVal := m.Run()
|
||||||
|
|
||||||
|
// Delete test bucket.
|
||||||
|
_ = testStore.DeleteBucket(bucket)
|
||||||
|
|
||||||
|
os.Exit(exitVal)
|
||||||
}
|
}
|
||||||
|
6
s3/s3.go
6
s3/s3.go
@@ -159,7 +159,7 @@ func (s *Storage) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return database client
|
// Conn returns database client.
|
||||||
func (s *Storage) Conn() *s3.Client {
|
func (s *Storage) Conn() *s3.Client {
|
||||||
return s.svc
|
return s.svc
|
||||||
}
|
}
|
||||||
@@ -186,11 +186,11 @@ func returnAWSConfig(cfg Config) (aws.Config, error) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if cfg.Credentials != (Credentials{}) {
|
if cfg.Credentials != (Credentials{}) {
|
||||||
credentials := credentials.NewStaticCredentialsProvider(cfg.Credentials.AccessKey, cfg.Credentials.SecretAccessKey, "")
|
creds := credentials.NewStaticCredentialsProvider(cfg.Credentials.AccessKey, cfg.Credentials.SecretAccessKey, "")
|
||||||
return awsconfig.LoadDefaultConfig(context.TODO(),
|
return awsconfig.LoadDefaultConfig(context.TODO(),
|
||||||
awsconfig.WithRegion(cfg.Region),
|
awsconfig.WithRegion(cfg.Region),
|
||||||
awsconfig.WithEndpointResolverWithOptions(endpoint),
|
awsconfig.WithEndpointResolverWithOptions(endpoint),
|
||||||
awsconfig.WithCredentialsProvider(credentials),
|
awsconfig.WithCredentialsProvider(creds),
|
||||||
awsconfig.WithRetryer(func() aws.Retryer {
|
awsconfig.WithRetryer(func() aws.Retryer {
|
||||||
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
|
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
|
||||||
}),
|
}),
|
||||||
|
@@ -2,6 +2,7 @@ package s3
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go-v2/aws"
|
"github.com/aws/aws-sdk-go-v2/aws"
|
||||||
@@ -11,6 +12,29 @@ import (
|
|||||||
|
|
||||||
// Additional methods for S3, but not required by gofiber Storage interface.
|
// Additional methods for S3, but not required by gofiber Storage interface.
|
||||||
|
|
||||||
|
// CreateBucket creates a new bucket.
|
||||||
|
func (s *Storage) CreateBucket(bucket string) error {
|
||||||
|
ctx, cancel := s.requestContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
_, err := s.svc.CreateBucket(ctx, &s3.CreateBucketInput{
|
||||||
|
Bucket: aws.String(bucket),
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Storage) DeleteBucket(bucket string) error {
|
||||||
|
ctx, cancel := s.requestContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
_, err := s.svc.DeleteBucket(ctx, &s3.DeleteBucketInput{
|
||||||
|
Bucket: aws.String(bucket),
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// SetWithChecksum sets key with value and checksum.
|
// SetWithChecksum sets key with value and checksum.
|
||||||
//
|
//
|
||||||
// Currently 4 algorithms are supported:
|
// Currently 4 algorithms are supported:
|
||||||
@@ -25,31 +49,34 @@ func (s *Storage) SetWithChecksum(key string, val []byte, checksum map[types.Che
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := s.requestContext()
|
poi := s3.PutObjectInput{
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
poi := &s3.PutObjectInput{
|
|
||||||
Bucket: &s.bucket,
|
Bucket: &s.bucket,
|
||||||
Key: aws.String(key),
|
Key: aws.String(key),
|
||||||
Body: bytes.NewReader(val),
|
Body: bytes.NewReader(val),
|
||||||
}
|
}
|
||||||
|
|
||||||
for alg, sum := range checksum {
|
for alg, sum := range checksum {
|
||||||
|
// S3 requires base64 encoded checksum.
|
||||||
|
b64str := base64.StdEncoding.EncodeToString(sum)
|
||||||
|
|
||||||
switch alg {
|
switch alg {
|
||||||
case types.ChecksumAlgorithmCrc32:
|
case types.ChecksumAlgorithmCrc32:
|
||||||
poi.ChecksumCRC32 = aws.String(string(sum))
|
poi.ChecksumCRC32 = aws.String(b64str)
|
||||||
case types.ChecksumAlgorithmCrc32c:
|
case types.ChecksumAlgorithmCrc32c:
|
||||||
poi.ChecksumCRC32C = aws.String(string(sum))
|
poi.ChecksumCRC32C = aws.String(b64str)
|
||||||
case types.ChecksumAlgorithmSha1:
|
case types.ChecksumAlgorithmSha1:
|
||||||
poi.ChecksumSHA1 = aws.String(string(sum))
|
poi.ChecksumSHA1 = aws.String(b64str)
|
||||||
case types.ChecksumAlgorithmSha256:
|
case types.ChecksumAlgorithmSha256:
|
||||||
poi.ChecksumSHA256 = aws.String(string(sum))
|
poi.ChecksumSHA256 = aws.String(b64str)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("invalid checksum algorithm: %s", alg)
|
return fmt.Errorf("invalid checksum algorithm: %s", alg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := s.uploader.Upload(ctx, poi)
|
ctx, cancel := s.requestContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
_, err := s.uploader.Upload(ctx, &poi)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@@ -8,20 +8,43 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Test_S3_CreateDeleteBucket(t *testing.T) {
|
||||||
|
bkt := "test-new-bucket"
|
||||||
|
|
||||||
|
err := testStore.CreateBucket(bkt)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = testStore.DeleteBucket(bkt)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func Test_S3_SetWithChecksum(t *testing.T) {
|
func Test_S3_SetWithChecksum(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
key = "set-with-checksum"
|
key = "set-with-checksum"
|
||||||
val = []byte("doe")
|
val = []byte("doe")
|
||||||
sha256sum = sha256.New().Sum(val)
|
|
||||||
checksum = map[types.ChecksumAlgorithm][]byte{
|
|
||||||
types.ChecksumAlgorithmSha256: sha256sum,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Create SHA-256 hash and get checksum.
|
||||||
|
sha256Hash := sha256.New()
|
||||||
|
sha256Hash.Write(val)
|
||||||
|
sha256sum := sha256Hash.Sum(nil)
|
||||||
|
|
||||||
|
checksum := map[types.ChecksumAlgorithm][]byte{
|
||||||
|
types.ChecksumAlgorithmSha256: sha256sum,
|
||||||
|
}
|
||||||
|
|
||||||
err := testStore.SetWithChecksum(key, val, checksum)
|
err := testStore.SetWithChecksum(key, val, checksum)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
result, err := testStore.Get(key)
|
result, err := testStore.Get(key)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, sha256sum, sha256.New().Sum(result))
|
|
||||||
|
// Compare value.
|
||||||
|
require.Equal(t, result, val)
|
||||||
|
|
||||||
|
// Compare checksum.
|
||||||
|
hash2 := sha256.New()
|
||||||
|
hash2.Write(result)
|
||||||
|
sha256sum2 := hash2.Sum(nil)
|
||||||
|
require.Equal(t, sha256sum, sha256sum2)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user