-
Notifications
You must be signed in to change notification settings - Fork 0
feat: S3 setup #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
feat: S3 setup #83
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f1d1744
Add S3 storage configuration and service
spokonya 124ca04
finished s3 setup and added testing handler and endpoint
spokonya 21ae351
chore: tidy go.mod dependencies
spokonya 3871a29
go.mod tidy pt2
spokonya 03b47ad
github slack workflow
spokonya 9f72dc0
Fix invalid timezone in users_tests.go
spokonya 75e803b
addressing comments except for contType param
spokonya cf0e38c
merge conflicts
spokonya b54dd11
Merge branch 'main' into s3-setup
danctila 31bd956
go formatted code
spokonya 8e5dcbf
ci fail fix
spokonya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,8 @@ | ||
| package config | ||
|
|
||
| type S3 struct { | ||
| AccessKeyID string `env:"ACCESS_KEY_ID,required"` | ||
| SecretAccessKey string `env:"SECRET_ACCESS_KEY,required"` | ||
| Region string `env:"REGION,required"` | ||
| BucketName string `env:"BUCKET_NAME,required"` | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,47 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/generate/selfserve/internal/errs" | ||
| s3storage "github.com/generate/selfserve/internal/service/s3" | ||
| "github.com/gofiber/fiber/v2" | ||
| ) | ||
|
|
||
| const expirationTime = 5 * time.Minute // Moved to a package level constant for reusability | ||
|
|
||
| type S3Handler struct { | ||
| S3Storage *s3storage.Storage | ||
| } | ||
|
|
||
| func NewS3Handler(s3Storage *s3storage.Storage) *S3Handler { | ||
| return &S3Handler{S3Storage: s3Storage} | ||
| } | ||
|
|
||
| // GeneratePresignedURL godoc | ||
| // @Summary Generate a presigned URL for a file | ||
| // @Description Generates a presigned URL for a file | ||
| // @Tags s3 | ||
| // @Accept json | ||
| // @Produce json | ||
| // @Param key path string true "File key" | ||
| // @Success 200 {object} map[string]string "Presigned URL response" | ||
| // @Failure 400 {object} map[string]string | ||
| // @Failure 500 {object} map[string]string | ||
| // @Router /s3/presigned-url/{key} [get] | ||
|
|
||
| func (h *S3Handler) GeneratePresignedURL(c *fiber.Ctx) error { | ||
| key := c.Params("key") | ||
| if key == "" { | ||
| return errs.BadRequest("key is required") | ||
| } | ||
|
|
||
| presignedURL, err := h.S3Storage.GeneratePresignedURL(c.Context(), key, expirationTime) | ||
| if err != nil { | ||
| return errs.InternalServerError() | ||
| } | ||
|
|
||
| return c.JSON(fiber.Map{ | ||
| "presigned_url": presignedURL, | ||
| }) | ||
| } |
This file contains hidden or 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,80 @@ | ||
| package s3 | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
| awsConfig "github.com/aws/aws-sdk-go-v2/config" | ||
| "github.com/aws/aws-sdk-go-v2/credentials" | ||
| "github.com/aws/aws-sdk-go-v2/service/s3" | ||
| "github.com/generate/selfserve/config" | ||
| ) | ||
|
|
||
| type Storage struct { | ||
| Client *s3.Client | ||
| BucketName string | ||
| URL *s3.PresignClient | ||
| } | ||
|
|
||
| func NewS3Storage(cfg config.S3) (*Storage, error) { | ||
|
spokonya marked this conversation as resolved.
|
||
| // Create AWS config with your credentials | ||
| awsCfg, err := awsConfig.LoadDefaultConfig(context.Background(), | ||
| awsConfig.WithRegion(cfg.Region), | ||
| awsConfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider( | ||
| cfg.AccessKeyID, | ||
| cfg.SecretAccessKey, | ||
| "", | ||
| )), | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create AWS config: %w", err) | ||
| } | ||
|
|
||
| // Create S3 client | ||
| client := s3.NewFromConfig(awsCfg) | ||
|
|
||
| return &Storage{ | ||
| Client: client, | ||
| BucketName: cfg.BucketName, | ||
| URL: s3.NewPresignClient(client), | ||
| }, nil | ||
| } | ||
|
|
||
| func (s *Storage) GeneratePresignedURL(ctx context.Context, key string, expiration time.Duration) (string, error) { | ||
|
spokonya marked this conversation as resolved.
|
||
| if key == "" { | ||
| return "", fmt.Errorf("key is required") | ||
| } | ||
| if expiration <= 0 { | ||
| return "", fmt.Errorf("expiration must be greater than 0") | ||
| } | ||
| presignedURL, err := s.URL.PresignPutObject(ctx, &s3.PutObjectInput{ | ||
| Bucket: aws.String(s.BucketName), | ||
| Key: aws.String(key), | ||
| }, func(opts *s3.PresignOptions) { | ||
| opts.Expires = expiration | ||
|
spokonya marked this conversation as resolved.
|
||
| }, | ||
| ) | ||
|
|
||
| if err != nil { | ||
| return "", fmt.Errorf("failed to generate presigned URL with key %s: %w", key, err) | ||
| } | ||
|
Dao-Ho marked this conversation as resolved.
|
||
|
|
||
| return presignedURL.URL, nil | ||
| } | ||
|
|
||
| func (s *Storage) DeleteFile(ctx context.Context, key string) error { | ||
| if key == "" { | ||
| return fmt.Errorf("key is required") | ||
| } | ||
| _, err := s.Client.DeleteObject(ctx, &s3.DeleteObjectInput{ | ||
| Bucket: aws.String(s.BucketName), | ||
| Key: aws.String(key), | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to delete file with key %s: %w", key, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the S3 storage location being under
storage/postgres/s3/might not make the most sense architecturally because S3 is not related to PostgresI would recommend:
Make sure to: