-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (59 loc) · 1.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"context"
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func main() {
// Load environment variables
bucketName := os.Getenv("S3_BUCKET_NAME")
if bucketName == "" {
log.Fatalf("S3_BUCKET_NAME environment variable is not set")
}
region := os.Getenv("AWS_REGION")
if region == "" {
region = "us-east-1" // Default region
}
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithHTTPClient(&http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}))
// Load AWS configuration
maxRetries := 120
for i := 0; i < maxRetries; i++ {
time.Sleep(time.Second)
// Create S3 client
s3Client := s3.NewFromConfig(cfg)
// Create uploader
uploader := manager.NewUploader(s3Client)
// File content
content := "Hello, World!"
fileKey := "hello-world.txt"
// Upload file
_, err = uploader.Upload(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(fileKey),
Body: strings.NewReader(content),
})
if err == nil {
fmt.Printf("File uploaded successfully: %s\n", fileKey)
time.Sleep(10000 * time.Second)
}
fmt.Printf("Failed to upload file: %v\n", err)
if i == maxRetries-1 {
log.Fatalf("Failed to upload file after %d retries", maxRetries)
}
}
}