generated from VLADIMIR/template
add file storage module
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
module evening_detective_server
|
||||
|
||||
go 1.25.0
|
||||
go 1.26
|
||||
|
||||
require (
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0
|
||||
@@ -11,6 +11,25 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go-v2 v1.42.0 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.13 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/config v1.32.25 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.19.24 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.29 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/s3/transfermanager v0.2.11 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.29 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.29 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.30 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.12 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.22 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.29 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.29 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.104.0 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.2.0 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.31.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.36.6 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.43.3 // indirect
|
||||
github.com/aws/smithy-go v1.27.1 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
@@ -27,6 +46,7 @@ require (
|
||||
github.com/jackc/pgx/v5 v5.10.0
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/kzzan/s3kit v0.0.4
|
||||
github.com/swaggest/swgui v1.8.8
|
||||
golang.org/x/crypto v0.53.0
|
||||
golang.org/x/net v0.55.0 // indirect
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package file_storage
|
||||
|
||||
import "context"
|
||||
|
||||
type File struct {
|
||||
Name string
|
||||
Data []byte
|
||||
Mime string
|
||||
}
|
||||
|
||||
type IFileStorage interface {
|
||||
Put(ctx context.Context, file *File) error
|
||||
Get(ctx context.Context, filename string) (*File, error)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package file_storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"mime"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/kzzan/s3kit"
|
||||
)
|
||||
|
||||
type storage struct {
|
||||
client *s3kit.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
func NewRustFSStorage(
|
||||
endpoint string,
|
||||
accessKeyID string,
|
||||
secretAccessKey string,
|
||||
bucket string,
|
||||
) (IFileStorage, error) {
|
||||
client, err := s3kit.New(s3kit.Config{
|
||||
Endpoint: endpoint,
|
||||
AccessKeyID: accessKeyID,
|
||||
SecretAccessKey: secretAccessKey,
|
||||
Region: "ru-1",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &storage{
|
||||
client: client,
|
||||
bucket: bucket,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *storage) Put(ctx context.Context, file *File) error {
|
||||
return s.client.PutObjectBytes(ctx, s.bucket, file.Name, file.Data, file.Mime)
|
||||
}
|
||||
|
||||
func (s *storage) Get(ctx context.Context, filename string) (*File, error) {
|
||||
data, err := s.client.GetObjectBytes(ctx, s.bucket, filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &File{
|
||||
Name: filename,
|
||||
Data: data,
|
||||
Mime: s.getMimeType(filename),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *storage) getMimeType(filename string) string {
|
||||
if mimeType := mime.TypeByExtension(filepath.Ext(filename)); mimeType != "" {
|
||||
return mimeType
|
||||
}
|
||||
return "application/octet-stream"
|
||||
}
|
||||
Reference in New Issue
Block a user