add file storage module

This commit is contained in:
2026-07-11 13:54:12 +07:00
parent bf46fb4e81
commit 2ecc6ecd6a
3 changed files with 93 additions and 1 deletions
@@ -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)
}
+58
View File
@@ -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"
}