Skip to content

Instantly share code, notes, and snippets.

View hernanhrm's full-sized avatar
🏠
Working from home

Hernan Reyes hernanhrm

🏠
Working from home
View GitHub Profile
func main() {
var warriors = Warriors{}
warriors = append(warriors, NewMRSatan())
warriors = append(warriors, NewGoku())
executeWithoutIPS(warriors)
}
type Goku struct{}
func NewGoku() *Goku {
return &Goku{}
}
func (g Goku) Kick() {
println("Goku kicks")
}
type MRSatan struct{}
func NewMRSatan() *MRSatan {
return &MRSatan{}
}
func (m MRSatan) Kick() {
println("MRSatan kicks")
}
type Warrior interface {
Kick()
Punch()
Transform()
}
type Warriors []Warrior
func executeWithoutIPS(warriors Warriors) {
for _, warrior := range warriors {
// 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)
}
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)
}
// 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 {
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),
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
// 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 {