This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (79 loc) · 1.97 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"fmt"
"os"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go name-of-bucket")
return
}
bucket := os.Args[1]
client := getClient()
// Run this test twice, so we can see the first upload
// always succeeds and the second always fails.
doUploads(client, bucket)
doUploads(client, bucket)
}
func doUploads(client *minio.Client, bucket string) {
goodPutOptions := getPutOptions("Metadata values with single spaces are OK")
badPutOptions := getPutOptions("Metadata values with two consecutive spaces cause upload to fail")
err := uploadFile(client, bucket, goodPutOptions)
if err != nil {
fmt.Println("Upload with goodPutOptions FAILED with error:", err)
} else {
fmt.Println("Upload with goodPutOptions SUCCEEDED")
}
err = uploadFile(client, bucket, badPutOptions)
if err != nil {
fmt.Println("Upload with badPutOptions FAILED with error:", err)
} else {
fmt.Println("Upload with badPutOptions SUCCEEDED")
}
}
func getPutOptions(str string) minio.PutObjectOptions {
return minio.PutObjectOptions{
UserMetadata: map[string]string{
"custom-data": str,
},
ContentType: "text/plain",
}
}
func getClient() *minio.Client {
client, err := minio.New(
"s3.us-east-1.wasabisys.com",
&minio.Options{
Creds: credentials.NewStaticV4(getEnvVar("WASABI_ACCESS_KEY"), getEnvVar("WASABI_SECRET_KEY"), ""),
Secure: true,
})
if err != nil {
panic(err)
}
return client
}
func getEnvVar(name string) string {
value := os.Getenv(name)
if value == "" {
panic(fmt.Sprintf("Env var %s is not set", name))
}
return value
}
func uploadFile(client *minio.Client, bucket string, putOptions minio.PutObjectOptions) error {
file, err := os.Open("sample.txt")
if err != nil {
return err
}
defer file.Close()
_, err = client.PutObject(
context.Background(),
bucket,
"sample.txt",
file,
342,
putOptions,
)
return err
}