使用 AWS-SDK 上传文件到 MinIO

公司内网环境自建的 MinIO 作为对象存储,而外网的对象存储则是使用的阿里云服务

MinIO阿里云 都兼容亚马逊(AWS) 的S3协议, 为此使用 AWS-SDK 上传服务生成的数据内容

docker 创建一个 MinIO 的容器

MINIO_ROOT_USER 和 MINIO_ROOT_PASSWORD 就是 9001 端口的登录的账号密码, 也是AK和SK(也可以进入后台自己更换)

1
2
docker run -p 9000:9000 -p 9001:9001 --name minio1 -v ~/minio/data:/data -e "MINIO_ROOT_USER=AKIAIOSFODNN7EXAMPLE" -e "MINIO_ROOT_PASSWORD=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" quay.io/minio/minio server /data --console-address ":9001"

1
go get github.com/aws/aws-sdk-go@v1.44.70
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main

import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
_ "github.com/aws/aws-sdk-go/service/s3/s3manager"
"os"
)

func NewSess() *session.Session {
access_key := "AKIAIOSFODNN7EXAMPLE"
secret_key := "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
end_point := "http://192.168.163.121:9000" //endpoint设置, 不要动


sess, err := session.NewSession(&aws.Config{
// credentials.NewStaticCredentials 这里第三个参数是token, 在使用 aliyun-sdk 时会获取的对应的值
Credentials: credentials.NewStaticCredentials(access_key, secret_key, ""),
Endpoint: aws.String(end_point),
Region: aws.String("us-east-1"),
DisableSSL: aws.Bool(true),

/*
// false 会使用 virtual-host style方式, http://192.168.163.121:9000 -> http://bucket.192.168.163.121:9000
// true 会使用 强制使用路径方式, http://192.168.163.121:9000 -> http://192.168.163.121:9000/bucket
*/
S3ForcePathStyle: aws.Bool(true),
})

if err != nil {
panic(err)
}
return sess
}

func main() {


get_bucket(NewSess())
get_file_and_folder(NewSess(), "test")
getFile(NewSess(), "test", "新建 文本文档.txt")
uploadFile(NewSess(), "test", "新建 文本文档1.txt", "im data in 新建 文本文档1.txt")
}

func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"", args...)
os.Exit(1)
}

func get_bucket(sess *session.Session) {

svc := s3.New(sess)
result, err := svc.ListBuckets(nil)
if err != nil {
exitErrorf("Unable to list buckets, %v", err)
}

fmt.Println("Buckets:")

for _, b := range result.Buckets {
fmt.Printf("* %s created on %s",
aws.StringValue(b.Name), aws.TimeValue(b.CreationDate))
}

for _, b := range result.Buckets {
fmt.Printf("\n%s", aws.StringValue(b.Name))
}

}

func get_file_and_folder(sess *session.Session, bucket string) {


// bucket后跟, go run ....go bucketname
fmt.Println()
fmt.Println(bucket)

svc := s3.New(sess)

params := &s3.ListObjectsInput{
Bucket: aws.String(fmt.Sprint("/", bucket)),
Prefix: aws.String(""),
}
resp, err := svc.ListObjects(params)

if err != nil {
exitErrorf("Unable to list items in bucket %q, %v", bucket, err)
}

for _, item := range resp.Contents {
fmt.Println("Name: ", *item.Key)
fmt.Println("Last modified:", *item.LastModified)
fmt.Println("Size: ", *item.Size)
fmt.Println("Storage class:", *item.StorageClass)
fmt.Println("")
}

}

func getFile(sess *session.Session, bucket, item string) {


file, err := os.Create(item)
if err != nil {
exitErrorf("Unable to open file %q, %v", err)
}

defer file.Close()

downloader := s3manager.NewDownloader(sess)

numBytes, err := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(fmt.Sprint("/", bucket)),
Key: aws.String(item),
})
if err != nil {
exitErrorf("Unable to download item %q, %v", item, err)
}

fmt.Println("Downloaded", file.Name(), numBytes, "bytes")
}

func uploadFile(sess *session.Session, bucket string, filename, fileData string) {

uploader := s3manager.NewUploader(sess)

_, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(fmt.Sprint("/", bucket)),
Key: aws.String(filename),
Body: bytes.NewReader([]byte(fileData)),
})
if err != nil {
// Print the error and exit.
exitErrorf("Unable to upload %q to %q, %v", filename, bucket, err)
}

fmt.Printf("Successfully uploaded %q to %q ", filename, bucket)
}

使用 AWS-SDK 上传文件到 MinIO
https://maocat.cc/2022/03/06/blog/golang/aws-s3-oss-go/
发布于
2022年3月6日
许可协议