Skip to content

Commit

Permalink
add método DownloadFromS3
Browse files Browse the repository at this point in the history
  • Loading branch information
rayanemarusca committed Nov 20, 2023
1 parent cbdda3e commit 211b1e3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.DS_Store
# Dependency directories (remove the comment below to include it)
# vendor/
89 changes: 55 additions & 34 deletions aws_s3.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package goutils

import (
"bytes"
"encoding/base64"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"mime/multipart"
"strings"
"time"
"bytes"
"encoding/base64"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"mime/multipart"
"os"
"strings"
"time"
)

func UpdloadInS3(file multipart.File, path, fileName string) string {
Expand Down Expand Up @@ -141,30 +143,49 @@ func UpdloadInS3Base64Byte(b64 []byte, path, fileName string) string {
}

func UpdloadInS3ArqTxt(texto string, path, fileName string) string {
// The session the S3 Uploader will use
sess := ConnectAws()

// Caso tenha om PATH ai ele concatena
if path != "" {
fileName = fmt.Sprint(path, "/", fileName)
}

// Create an uploader with the session and default options
uploader := s3manager.NewUploader(sess)

myBucket := Godotenv("BUCKET_NAME")

//upload to the s3 bucket
up, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(myBucket),
ACL: aws.String("public-read"),
Key: aws.String(fileName),
Body: strings.NewReader(texto),
})
if err != nil {
CreateFileDay(Message{Error: err.Error()})
return ""
}
CreateFileDay(Message{Info: fmt.Sprint("Upload do Arquivo: ", up.UploadID)})
return "https://" + myBucket + "." + "s3.amazonaws.com/" + fileName
// The session the S3 Uploader will use
sess := ConnectAws()

// Caso tenha om PATH ai ele concatena
if path != "" {
fileName = fmt.Sprint(path, "/", fileName)
}

// Create an uploader with the session and default options
uploader := s3manager.NewUploader(sess)

myBucket := Godotenv("BUCKET_NAME")

//upload to the s3 bucket
up, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(myBucket),
ACL: aws.String("public-read"),
Key: aws.String(fileName),
Body: strings.NewReader(texto),
})
if err != nil {
CreateFileDay(Message{Error: err.Error()})
return ""
}
CreateFileDay(Message{Info: fmt.Sprint("Upload do Arquivo: ", up.UploadID)})
return "https://" + myBucket + "." + "s3.amazonaws.com/" + fileName
}

func DownloadFromS3(fileName, localFilePath string) error {
sess := ConnectAws()
downloader := s3.New(sess)

file, err := os.Create(localFilePath)
if err != nil {
return err
}
defer file.Close()

bucketName := Godotenv("BUCKET_NAME")
_, err = downloader.GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(fileName),
})

return err
}

0 comments on commit 211b1e3

Please sign in to comment.