forked from mtusi2026/mtusi2026lab4
ЛР4: S3-вложения, приватный bucket и согласованность хранилищ (трек 2)
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
// Command s3bootstrap creates the private attachment bucket idempotently and
|
||||
// proves that the configured administrative credentials can use it. It is a
|
||||
// one-shot compose service, separate from the API: API replicas never receive
|
||||
// the administrative credentials and cannot create buckets.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"mtusi.ru/messenger-lab4/internal/objectstore"
|
||||
)
|
||||
|
||||
func main() {
|
||||
endpoint := strings.TrimSpace(os.Getenv("S3_ENDPOINT"))
|
||||
bucket := strings.TrimSpace(os.Getenv("S3_BUCKET"))
|
||||
region := strings.TrimSpace(os.Getenv("S3_REGION"))
|
||||
if region == "" {
|
||||
region = "us-east-1"
|
||||
}
|
||||
accessKey := strings.TrimSpace(os.Getenv("S3_ADMIN_ACCESS_KEY"))
|
||||
secretKey := strings.TrimSpace(os.Getenv("S3_ADMIN_SECRET_KEY"))
|
||||
if endpoint == "" || bucket == "" || accessKey == "" || secretKey == "" {
|
||||
fmt.Fprintln(os.Stderr, "s3bootstrap: S3_ENDPOINT, S3_BUCKET, S3_ADMIN_ACCESS_KEY and S3_ADMIN_SECRET_KEY are required")
|
||||
os.Exit(2)
|
||||
}
|
||||
timeout := 30 * time.Second
|
||||
if raw := strings.TrimSpace(os.Getenv("S3_TIMEOUT_SECONDS")); raw != "" {
|
||||
seconds, err := strconv.Atoi(raw)
|
||||
if err != nil || seconds < 5 || seconds > 120 {
|
||||
fmt.Fprintln(os.Stderr, "s3bootstrap: S3_TIMEOUT_SECONDS must be an integer between 5 and 120")
|
||||
os.Exit(2)
|
||||
}
|
||||
timeout = time.Duration(seconds) * time.Second
|
||||
}
|
||||
store, err := objectstore.Open(objectstore.Config{
|
||||
Endpoint: endpoint,
|
||||
Region: region,
|
||||
Bucket: bucket,
|
||||
AccessKey: accessKey,
|
||||
SecretKey: secretKey,
|
||||
Timeout: timeout,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "s3bootstrap: configuration error:", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
created, err := store.EnsureBucket(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "s3bootstrap: bucket creation failed:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := store.Probe(ctx); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "s3bootstrap: credential/bucket probe failed:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
result := map[string]any{
|
||||
"bucket": bucket,
|
||||
"created": created,
|
||||
"verified": true,
|
||||
}
|
||||
encoded, _ := json.Marshal(result)
|
||||
fmt.Println(string(encoded))
|
||||
}
|
||||
Reference in New Issue
Block a user