MCPcopy
hub / github.com/gobyexample-cn/gobyexample / main

Function main

tools/upload.go:35–83  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

33}
34
35func main() {
36 region := flag.String("region", "", "S3 region")
37 bucket := flag.String("bucket", "", "S3 bucket name")
38 flag.Parse()
39
40 if len(*region) == 0 || len(*bucket) == 0 {
41 log.Fatalf("region and bucket must be specified [region=%s, bucket=%s]", *region, *bucket)
42 }
43
44 cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(*region))
45 if err != nil {
46 log.Fatal(err)
47 }
48
49 client := s3.NewFromConfig(cfg)
50
51 // The whole contents of the public/ directory are uploaded. This code assumes
52 // the directory structure is flat - there are no subdirectories.
53 publicDir := "./public/"
54 c, err := os.ReadDir(publicDir)
55 if err != nil {
56 log.Fatal(err)
57 }
58
59 for _, entry := range c {
60 if !entry.IsDir() {
61 file, err := os.Open(filepath.Join(publicDir, entry.Name()))
62 if err != nil {
63 log.Fatal(err)
64 }
65 defer file.Close()
66
67 contentType := guessContentType(entry.Name())
68 log.Printf("Uploading %s (%s)", entry.Name(), contentType)
69
70 cfg := &s3.PutObjectInput{
71 Bucket: bucket,
72 Key: aws.String(entry.Name()),
73 Body: file,
74 ContentType: aws.String(contentType),
75 }
76
77 _, err = client.PutObject(context.TODO(), cfg)
78 if err != nil {
79 log.Fatal(err)
80 }
81 }
82 }
83}

Callers

nothing calls this directly

Calls 2

guessContentTypeFunction · 0.85
StringMethod · 0.80

Tested by

no test coverage detected