atri.dad/lib/s3.go

126 lines
2.7 KiB
Go
Raw Normal View History

2023-05-18 20:04:55 -06:00
package lib
import (
"fmt"
"os"
2024-04-01 02:14:28 -06:00
"time"
"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"
2023-05-18 20:04:55 -06:00
)
2024-08-05 12:40:07 -06:00
// Generates a presigned URL for the given key. Returns the pre-signed URL.
2023-05-18 20:04:55 -06:00
func GeneratePublicURL(key string) string {
2024-08-05 12:40:07 -06:00
// Get the S3 bucket name
2023-05-18 20:04:55 -06:00
bucket := os.Getenv("BUCKET_NAME")
2024-04-01 02:14:28 -06:00
if bucket == "" {
2024-08-05 12:40:07 -06:00
fmt.Println("No S3 bucket specified.")
2024-04-01 02:14:28 -06:00
return ""
}
2024-08-05 12:40:07 -06:00
// Create the S3 session
2023-05-18 20:04:55 -06:00
endpoint := os.Getenv("AWS_ENDPOINT_URL_S3")
2024-04-01 02:14:28 -06:00
accessKeyID := os.Getenv("AWS_ACCESS_KEY_ID")
secretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
region := os.Getenv("AWS_REGION")
sess, err := session.NewSession(&aws.Config{
Region: &region,
Credentials: credentials.NewStaticCredentials(
accessKeyID,
secretAccessKey,
"",
),
Endpoint: aws.String(endpoint),
})
if err != nil {
return ""
}
svc := s3.New(sess)
2024-08-05 12:40:07 -06:00
// Generate the presigned URL
2024-04-01 02:14:28 -06:00
req, _ := svc.GetObjectRequest(&s3.GetObjectInput{
Bucket: &bucket,
Key: &key,
})
urlStr, err := req.Presign(15 * time.Minute)
if err != nil {
return ""
}
2023-05-18 20:04:55 -06:00
2024-04-01 02:14:28 -06:00
return urlStr
2023-05-18 20:04:55 -06:00
}
2024-08-05 12:40:07 -06:00
// Generates a list of presigned URLs for all items in the given directory. Returns an array of pre-signed URLs.
func GeneratePublicURLsFromDirectory(directory string) []string {
// Get the S3 bucket name
bucket := os.Getenv("BUCKET_NAME")
if bucket == "" {
fmt.Println("No S3 bucket specified.")
return []string{}
}
// Create the S3 session
endpoint := os.Getenv("AWS_ENDPOINT_URL_S3")
accessKeyID := os.Getenv("AWS_ACCESS_KEY_ID")
secretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
region := os.Getenv("AWS_REGION")
sess, err := session.NewSession(&aws.Config{
Region: &region,
Credentials: credentials.NewStaticCredentials(
accessKeyID,
secretAccessKey,
"",
),
Endpoint: aws.String(endpoint),
})
if err != nil {
return []string{}
}
svc := s3.New(sess)
// Create the input for the ListObjectsV2 call
input := &s3.ListObjectsV2Input{
Bucket: aws.String(bucket),
Prefix: aws.String(directory),
}
// Get the list of items in the directory
result, err := svc.ListObjectsV2(input)
if err != nil {
return []string{}
}
//Remove the items in results that are directories
for i := 0; i < len(result.Contents); i++ {
if *result.Contents[i].Key == directory {
result.Contents = append(result.Contents[:i], result.Contents[i+1:]...)
i--
}
}
// Generate the presigned URLs
urls := []string{}
for _, item := range result.Contents {
req, _ := svc.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: item.Key,
})
urlStr, err := req.Presign(15 * time.Minute)
if err != nil {
continue
}
urls = append(urls, urlStr)
}
return urls
}