everytab/pipeline/05_bundle_gen/s3.go

48 lines
1.2 KiB
Go

package main
import (
"bytes"
"context"
"os"
"path/filepath"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
var s3Client *s3.Client
func initS3() error {
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion("us-east-1"))
if err != nil {
return err
}
s3Client = s3.NewFromConfig(cfg)
return nil
}
// iconHashToPath converts a SHA-256 hash to a sharded file path.
func iconHashToPath(iconsDir, hash string) string {
if len(hash) < 6 {
return filepath.Join(iconsDir, hash)
}
return filepath.Join(iconsDir, hash[:2], hash[2:4], hash[4:6], hash)
}
// readIconFromDisk reads an icon file from the local sharded directory.
func readIconFromDisk(iconsDir, hash string) ([]byte, error) {
return os.ReadFile(iconHashToPath(iconsDir, hash))
}
// s3UploadBundle uploads a bundle JSON to S3.
func s3UploadBundle(bucket, key string, data []byte) error {
_, err := s3Client.PutObject(context.Background(), &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: bytes.NewReader(data),
ContentType: aws.String("application/json"),
})
return err
}