View without-ips.go
This file contains 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
func main() { | |
var warriors = Warriors{} | |
warriors = append(warriors, NewMRSatan()) | |
warriors = append(warriors, NewGoku()) | |
executeWithoutIPS(warriors) | |
} |
View goku.go
This file contains 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
type Goku struct{} | |
func NewGoku() *Goku { | |
return &Goku{} | |
} | |
func (g Goku) Kick() { | |
println("Goku kicks") | |
} |
View satan.go
This file contains 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
type MRSatan struct{} | |
func NewMRSatan() *MRSatan { | |
return &MRSatan{} | |
} | |
func (m MRSatan) Kick() { | |
println("MRSatan kicks") | |
} |
View warrior.go
This file contains 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
type Warrior interface { | |
Kick() | |
Punch() | |
Transform() | |
} | |
type Warriors []Warrior | |
func executeWithoutIPS(warriors Warriors) { | |
for _, warrior := range warriors { |
View getemployees.go
This file contains 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
// GetEmployees returns a list of employees with a pre-signed URL to their profile picture | |
// to make the example easier to follow, I have a mock data with a list of employees, | |
// so we don't need to use a database | |
func (u FileManager) GetEmployees() ([]Employee, error) { | |
for i, _ := range employees { | |
preSignedURL, err := u.service.Presign(employees[i].Picture) | |
if err != nil { | |
return nil, fmt.Errorf("filemanager.GetEmployees(): %w", err) | |
} |
View presignv2.go
This file contains 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
func (s Service) PresignV2(key string) (string, error) { | |
req, _ := s.S3.GetObjectRequest(&s3.GetObjectInput{ | |
Bucket: aws.String(s.Bucket), | |
Key: aws.String(key), | |
}) | |
signedURL, err := req.Presign(time.Minute) | |
if err != nil { | |
return "", fmt.Errorf("s3.SignKey(): %v", err) | |
} |
View presign.go
This file contains 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
// Presign signs a key object with an expiry time | |
func (s Service) Presign(key string) (string, error) { | |
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s.s3.amazonaws.com/%s", s.Bucket, key), nil) | |
if err != nil { | |
return "", fmt.Errorf("s3.Presign.http.NewRequest(): %w", err) | |
} | |
// we sign the request with the signTime and a time expiration of 1 day | |
_, err = s.Signer.Presign(req, nil, "s3", *s.Session.Config.Region, time.Hour*24, getSignTime()) | |
if err != nil { |
View getfile.go
This file contains 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
type GetFileResponse struct { | |
FileBytes []byte | |
ContentType string | |
} | |
// GetFile returns a file from the s3 bucket | |
func (s Service) GetFile(filename string) (GetFileResponse, error) { | |
input := &s3.GetObjectInput{ | |
Bucket: aws.String(s.Bucket), | |
Key: aws.String(filename), |
View upload.go
This file contains 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
const ( | |
// PublicRead is used to set the permission of a file to public read | |
PublicRead = "public-read" | |
// Private is used to set the permission of a file to private | |
Private = "private" | |
) | |
// Upload takes a file and uploads it to the s3 bucket | |
func (s Service) Upload(fileBytes []byte, contentType, path string, isPublic bool) error { | |
permission := PublicRead |
View constructor.go
This file contains 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
// Config contains the configuration read from the .env file | |
type Config struct { | |
S3AccessKey string `json:"s3_access_key"` | |
S3SecretKey string `json:"s3_secret_key"` | |
S3BucketName string `json:"s3_bucket_name"` | |
S3BucketRegion string `json:"s3_bucket_region"` | |
} | |
// Service is the use case of the storage s3 service | |
type Service struct { |
NewerOlder