Removed S3

This commit is contained in:
2024-09-08 23:42:36 -06:00
parent c0923aaf2b
commit 2edff6915a
15 changed files with 51 additions and 203 deletions

33
lib/files.go Normal file
View File

@ -0,0 +1,33 @@
package lib
import (
"os"
"path/filepath"
)
// ListFiles returns a slice of file paths in the given directory
func ListFiles(dir string) ([]string, error) {
var filePaths []string
entries, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
for _, entry := range entries {
if entry.IsDir() {
// Recursively list files in subdirectories
subDir := filepath.Join(dir, entry.Name())
subFiles, err := ListFiles(subDir)
if err != nil {
return nil, err
}
filePaths = append(filePaths, subFiles...)
} else {
// Add file path to the slice
filePath := filepath.Join(dir, entry.Name())
filePaths = append(filePaths, filePath)
}
}
return filePaths, nil
}

125
lib/s3.go
View File

@ -1,125 +0,0 @@
package lib
import (
"fmt"
"os"
"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"
)
// Generates a presigned URL for the given key. Returns the pre-signed URL.
func GeneratePublicURL(key string) string {
// Get the S3 bucket name
bucket := os.Getenv("BUCKET_NAME")
if bucket == "" {
fmt.Println("No S3 bucket specified.")
return ""
}
// 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 ""
}
svc := s3.New(sess)
// Generate the presigned URL
req, _ := svc.GetObjectRequest(&s3.GetObjectInput{
Bucket: &bucket,
Key: &key,
})
urlStr, err := req.Presign(15 * time.Minute)
if err != nil {
return ""
}
return urlStr
}
// 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
}