Skip to content

Commit

Permalink
finished Presigned URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Mar 4, 2024
1 parent 21f687f commit 86d5e2b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 30 deletions.
18 changes: 0 additions & 18 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,6 @@ func InitConfig() {

// create a s3 client session
S3Client = s3.NewFromConfig(awsConfig)

count := 10
fmt.Printf("Let's list up to %v buckets for your account.\n", count)
result, err := S3Client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
if err != nil {
fmt.Printf("Couldn't list buckets for your account. Here's why: %v\n", err)
return
}
if len(result.Buckets) == 0 {
fmt.Println("You don't have any buckets!")
} else {
if count > len(result.Buckets) {
count = len(result.Buckets)
}
for _, bucket := range result.Buckets[:count] {
fmt.Printf("\t%v\n", *bucket.Name)
}
}
PresignClient = s3.NewPresignClient(S3Client)

// only make this call if there is a Relay auth key
Expand Down
2 changes: 1 addition & 1 deletion db/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request)
} else {
orderQuery = " ORDER BY " + sortBy + "" + "DESC"
}
if limit > 0 {
if limit > 1 {
limitQuery = fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset)
}

Expand Down
65 changes: 54 additions & 11 deletions handlers/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,19 +380,62 @@ func UploadMetricsCsv(data [][]string, request db.PaymentDateRange) (error, stri

key := fmt.Sprintf("metrics%s-%s.csv", request.StartDate, request.EndDate)
path := fmt.Sprintf("%s/%s", config.S3FolderName, key)
_, err = config.S3Client.PutObject(context.Background(), &s3.PutObjectInput{
Bucket: aws.String(config.S3BucketName),
Key: aws.String(path),
Body: bytes.NewReader(fileBuffer),
ContentLength: aws.Int64(fileSize),
ContentType: aws.String("application/csv"),
ContentDisposition: aws.String("attachment"),
ServerSideEncryption: "AES256",
})

err, postPresignedUrl := createPresignedUrl(path)

if err != nil {
fmt.Println("Presigned Error", err)
}

r, err := http.NewRequest(http.MethodPut, postPresignedUrl, bytes.NewReader(fileBuffer))
if err != nil {
fmt.Println("Posting presign s3 error:", err)
}
r.Header.Set("Content-Type", "multipart/form-data")
client := &http.Client{}
_, err = client.Do(r)

if err != nil {
fmt.Println("Error occured while posting presigned URL", err)
}

// Delete image from uploads folder
DeleteFileFromUploadsFolder(filePath)

url := fmt.Sprintf("%s/%s/%s", config.S3Url, config.S3FolderName, key)
return err, url
err, presignedUrlGet := getPresignedUrl(path)

return err, presignedUrlGet
}

func createPresignedUrl(path string) (error, string) {
presignedUrl, err := config.PresignClient.PresignPutObject(context.Background(),
&s3.PutObjectInput{
Bucket: aws.String(config.S3BucketName),
Key: aws.String(path),
},
s3.WithPresignExpires(time.Minute*15),
)

if err != nil {
return err, ""
}

return nil, presignedUrl.URL
}

func getPresignedUrl(path string) (error, string) {
presignedUrl, err := config.PresignClient.PresignGetObject(context.Background(),
&s3.GetObjectInput{
Bucket: aws.String(config.S3BucketName),
Key: aws.String(path),
ResponseContentDisposition: aws.String("attachment"),
},
s3.WithPresignExpires(time.Minute*15),
)

if err != nil {
return err, ""
}

return nil, presignedUrl.URL
}

0 comments on commit 86d5e2b

Please sign in to comment.