mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Implement s3 storage.
Use uuid as file id.
This commit is contained in:
@@ -11,7 +11,7 @@ type LocalStorage struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func (s *LocalStorage) Upload(filePath string) (string, error) {
|
||||
func (s *LocalStorage) Upload(filePath string, _ string) (string, error) {
|
||||
id := uuid.New().String()
|
||||
inputPath := s.Path + "/" + id
|
||||
input, err := os.OpenFile(inputPath, os.O_RDWR|os.O_CREATE, 0755)
|
||||
@@ -31,7 +31,7 @@ func (s *LocalStorage) Upload(filePath string) (string, error) {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *LocalStorage) Download(storageKey string) (string, error) {
|
||||
func (s *LocalStorage) Download(storageKey string, _ string) (string, error) {
|
||||
path := s.Path + "/" + storageKey
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return "", ErrFileUnavailable
|
||||
|
@@ -1,8 +1,16 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type S3Storage struct {
|
||||
@@ -12,14 +20,45 @@ type S3Storage struct {
|
||||
BucketName string
|
||||
}
|
||||
|
||||
func (s *S3Storage) Upload(filePath string) (string, error) {
|
||||
// TODO: Implement S3 upload logic here
|
||||
return "", nil
|
||||
func (s *S3Storage) Upload(filePath string, fileName string) (string, error) {
|
||||
minioClient, err := minio.New(s.EndPoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(s.AccessKeyID, s.SecretAccessKey, ""),
|
||||
Secure: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Error("Failed to create S3 client: ", err)
|
||||
return "", errors.New("failed to create S3 client")
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
objectKey := uuid.NewString()
|
||||
objectKey += "/" + fileName
|
||||
_, err = minioClient.FPutObject(ctx, s.BucketName, objectKey, filePath, minio.PutObjectOptions{})
|
||||
if err != nil {
|
||||
log.Error("Failed to upload file to S3: ", err)
|
||||
return "", errors.New("failed to upload file to S3")
|
||||
}
|
||||
|
||||
return objectKey, nil
|
||||
}
|
||||
|
||||
func (s *S3Storage) Download(storageKey string) (string, error) {
|
||||
// TODO: Implement S3 download logic here
|
||||
return "", nil
|
||||
func (s *S3Storage) Download(storageKey string, fileName string) (string, error) {
|
||||
minioClient, err := minio.New(s.EndPoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(s.AccessKeyID, s.SecretAccessKey, ""),
|
||||
Secure: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Error("Failed to create S3 client: ", err)
|
||||
return "", errors.New("failed to create S3 client")
|
||||
}
|
||||
reqParams := make(url.Values)
|
||||
reqParams.Set("response-content-disposition", "attachment; filename=\""+fileName+"\"")
|
||||
presignedURL, err := minioClient.PresignedGetObject(context.Background(), s.BucketName, storageKey, 5*time.Second, reqParams)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return "", errors.New("failed to generate presigned URL")
|
||||
}
|
||||
return presignedURL.String(), nil
|
||||
}
|
||||
|
||||
func (s *S3Storage) Delete(storageKey string) error {
|
||||
|
@@ -13,9 +13,9 @@ var (
|
||||
|
||||
type IStorage interface {
|
||||
// Upload uploads a file to the storage and returns the storage key.
|
||||
Upload(filePath string) (string, error)
|
||||
Upload(filePath string, fileName string) (string, error)
|
||||
// Download return the download url of the file with the given storage key.
|
||||
Download(storageKey string) (string, error)
|
||||
Download(storageKey string, fileName string) (string, error)
|
||||
// Delete deletes the file with the given storage key.
|
||||
Delete(storageKey string) error
|
||||
// ToString returns the storage configuration as a string.
|
||||
|
Reference in New Issue
Block a user