-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #978 from iredmail/main
s3: New methods: CreateBucket, DeleteBucket, SetWithChecksum.
- Loading branch information
Showing
7 changed files
with
218 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package s3 | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
const ( | ||
bucket = "testbucket" | ||
) | ||
|
||
var testStore *Storage | ||
|
||
func TestMain(m *testing.M) { | ||
testStore = New( | ||
Config{ | ||
Bucket: bucket, | ||
Endpoint: "http://127.0.0.1:9000/", | ||
Region: "us-east-1", | ||
Credentials: Credentials{ | ||
AccessKey: "minioadmin", | ||
SecretAccessKey: "minioadmin", | ||
}, | ||
RequestTimeout: 3 * time.Second, | ||
}, | ||
) | ||
|
||
// Create test bucket. | ||
_ = testStore.CreateBucket(bucket) | ||
|
||
exitVal := m.Run() | ||
|
||
// Delete test bucket. | ||
_ = testStore.DeleteBucket(bucket) | ||
|
||
os.Exit(exitVal) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package s3 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"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. | ||
|
||
// 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 | ||
} | ||
|
||
// DeleteBucket deletes a bucket. | ||
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. | ||
// | ||
// Currently 4 algorithms 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[types.ChecksumAlgorithm][]byte) error { | ||
if len(key) <= 0 { | ||
return nil | ||
} | ||
|
||
poi := s3.PutObjectInput{ | ||
Bucket: &s.bucket, | ||
Key: aws.String(key), | ||
Body: bytes.NewReader(val), | ||
} | ||
|
||
for alg, sum := range checksum { | ||
// S3 requires base64 encoded checksum. | ||
b64str := base64.StdEncoding.EncodeToString(sum) | ||
|
||
switch alg { | ||
case types.ChecksumAlgorithmCrc32: | ||
poi.ChecksumCRC32 = aws.String(b64str) | ||
case types.ChecksumAlgorithmCrc32c: | ||
poi.ChecksumCRC32C = aws.String(b64str) | ||
case types.ChecksumAlgorithmSha1: | ||
poi.ChecksumSHA1 = aws.String(b64str) | ||
case types.ChecksumAlgorithmSha256: | ||
poi.ChecksumSHA256 = aws.String(b64str) | ||
default: | ||
return fmt.Errorf("invalid checksum algorithm: %s", alg) | ||
} | ||
} | ||
|
||
ctx, cancel := s.requestContext() | ||
defer cancel() | ||
|
||
_, err := s.uploader.Upload(ctx, &poi) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package s3 | ||
|
||
import ( | ||
"crypto/sha256" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/s3/types" | ||
"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) { | ||
var ( | ||
key = "set-with-checksum" | ||
val = []byte("doe") | ||
) | ||
|
||
// 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) | ||
require.NoError(t, err) | ||
|
||
result, err := testStore.Get(key) | ||
require.NoError(t, err) | ||
|
||
// Compare value. | ||
require.Equal(t, result, val) | ||
|
||
// Compare checksum. | ||
hash2 := sha256.New() | ||
hash2.Write(result) | ||
sha256sum2 := hash2.Sum(nil) | ||
require.Equal(t, sha256sum, sha256sum2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters