-
Notifications
You must be signed in to change notification settings - Fork 0
feature: add s3 infa #4
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package repository | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/aws/aws-sdk-go/aws" | ||
| "github.com/aws/aws-sdk-go/service/s3" | ||
| "github.com/google/uuid" | ||
|
|
||
| service "elearning/infra/s3" | ||
| ) | ||
|
|
||
| const ( | ||
| filePreSignExpireDuration = time.Hour * 12 | ||
| ) | ||
|
|
||
| type s3Repository struct { | ||
| service *service.S3Service | ||
| } | ||
|
|
||
| func NewS3Repository(service *service.S3Service) *s3Repository { | ||
| return &s3Repository{ | ||
| service: service, | ||
| } | ||
| } | ||
|
|
||
| type Image struct { | ||
| Id int `json:"id" gorm:"column:id;"` | ||
| Url string `json:"url" gorm:"column:url;"` | ||
| Width int `json:"width" gorm:"column:width;"` | ||
| Height int `json:"height" gorm:"column:height;"` | ||
|
Comment on lines
+34
to
+35
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tui vẫn chưa hình dung dc ông định dùng cái width với height của image như nào |
||
| CloudName string `json:"cloud_name,omitempty" gorm:"column:cloud_name;"` | ||
| } | ||
|
|
||
| func (repo *s3Repository) SaveImageUploaded(ctx context.Context, data []byte) (*Image, error) { | ||
| fileBytes := bytes.NewReader(data) | ||
| fileType := http.DetectContentType(data) | ||
|
|
||
| uploadID := uuid.New().String() | ||
|
|
||
| _, err := repo.service.PutObject(&s3.PutObjectInput{ | ||
| Bucket: aws.String(repo.service.BucketName), | ||
| Key: aws.String(uploadID), | ||
| ACL: aws.String("private"), | ||
| ContentType: aws.String(fileType), | ||
| Body: fileBytes, | ||
| }) | ||
|
Comment on lines
+39
to
+51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sao context ở đây k dc sử dụng, có lý do gì khiến ông k dùng |
||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| img := &Image{ | ||
| Url: fmt.Sprintf("%s/%s", repo.service.Domain, uploadID), | ||
| CloudName: "s3", | ||
| } | ||
|
|
||
| return img, err | ||
| } | ||
|
|
||
| func (repo *s3Repository) SaveFile(ctx context.Context, data []byte, dst string) (string, error) { | ||
| fileBytes := bytes.NewReader(data) | ||
| fileType := http.DetectContentType(data) | ||
|
|
||
| uploadID := uuid.New().String() | ||
|
|
||
| _, err := repo.service.PutObject(&s3.PutObjectInput{ | ||
| Bucket: aws.String(repo.service.BucketName), | ||
| Key: aws.String(uploadID), | ||
| ACL: aws.String("private"), | ||
| ContentType: aws.String(fileType), | ||
| Body: fileBytes, | ||
| }) | ||
|
Comment on lines
+65
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return uploadID, nil | ||
| } | ||
|
|
||
| func (repo *s3Repository) GetFileUrl(ctx context.Context, uploadID string) (string, error) { | ||
|
|
||
| req, _ := repo.service.GetObjectRequest(&s3.GetObjectInput{ | ||
| Bucket: aws.String(repo.service.BucketName), | ||
| Key: aws.String(uploadID), | ||
| }) | ||
|
|
||
| url, err := req.Presign(filePreSignExpireDuration) | ||
|
|
||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return url, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package s3 | ||
|
|
||
| import ( | ||
| "elearning/config" | ||
| "log" | ||
|
Comment on lines
+4
to
+5
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. standard package cần để lên đầu mọi thứ. |
||
|
|
||
| "github.com/aws/aws-sdk-go/aws" | ||
| "github.com/aws/aws-sdk-go/aws/credentials" | ||
| "github.com/aws/aws-sdk-go/aws/session" | ||
| "github.com/aws/aws-sdk-go/service/s3" | ||
| ) | ||
|
|
||
| type S3Service struct { | ||
| BucketName string | ||
| Domain string | ||
| *s3.S3 | ||
| } | ||
|
|
||
| func ConnectS3(cfg *config.Environment) (*S3Service, error) { | ||
| session, err := session.NewSession(&aws.Config{ | ||
| Region: aws.String(cfg.S3Region), | ||
| Credentials: credentials.NewStaticCredentials(cfg.S3ApiKey, cfg.S3SecretKey, ""), | ||
| }) | ||
|
|
||
| if err != nil { | ||
| log.Fatalln(err) | ||
| } | ||
|
|
||
| service := s3.New(session) | ||
|
|
||
| return &S3Service{cfg.S3BucketName, cfg.S3Domain, service}, nil | ||
| } | ||
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.
Make sure ông cập nhật bên file example
.env