Golang 调用 aws-sdk 操作 S3对象存储
前言
因为业务问题,要写一个S3对象存储管理代码,由于一直写Go,所以这次采用了Go,Go嘛,快,自带多线程,这种好处就不用多说了吧。
基础的功能
- 查看S3中包含的bucket
- bucket中的文件/文件夹
- bucket的删除
- bucket的创建
- bucket的文件上传
- bucket的文件下载
bucket的文件删除
aws-sdk-go的安装
玩Golang你还能不会那啥?对吧,那啥?那飞机!那飞机场,安上~
1
2 1go get github.com/aws/aws-sdk-go
2
aws-sdk-go 的基础使用
构建基础的S3连接
访问S3的时候,咱们需要access_key,secret_key,对象存储访问IP这三个参数,我们首先要创建一个aws的config,说白了,我们需要定义aws的配置,这样它才知道要怎么访问,去哪里访问等问题。
构建一个S3连接代码如下
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 1package main
2import (
3 "fmt"
4 "os"
5 "github.com/aws/aws-sdk-go/aws"
6 "github.com/aws/aws-sdk-go/aws/credentials"
7 _ "github.com/aws/aws-sdk-go/service/s3/s3manager"
8 "github.com/aws/aws-sdk-go/aws/session"
9 "github.com/aws/aws-sdk-go/service/s3"
10)
11func main() {
12
13 access_key := "xxxxxxxxxxxxx"
14 secret_key := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
15 end_point := "http://xx.xx.xx.xx:7480" //endpoint设置,不要动
16
17 sess, err := session.NewSession(&aws.Config{
18 Credentials: credentials.NewStaticCredentials(access_key, secret_key, ""),
19 Endpoint: aws.String(end_point),
20 Region: aws.String("us-east-1"),
21 DisableSSL: aws.Bool(true),
22 S3ForcePathStyle: aws.Bool(false), //virtual-host style方式,不要修改
23 })
24}
25
这时候需要你自己去定义一下access_key,secret_key,end_point这三个参数
接下来所有的代码,都是以这个连接模板,为核心,后面我就用同上代替配置,请注意!
所有的代码都传到GIT上了,到时候会给出地址,不懂得copy下来吧!
查看S3中包含的bucket
查看所有的bucket
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 1package main
2import (
3 导入包同上
4)
5
6func exitErrorf(msg string, args ...interface{}) {
7 fmt.Fprintf(os.Stderr, msg+"\n", args...)
8 os.Exit(1)
9}
10
11func main() {
12
13 配置同上
14
15 svc := s3.New(sess)
16 result, err := svc.ListBuckets(nil)
17 if err != nil {
18 exitErrorf("Unable to list buckets, %v", err)
19 }
20
21 fmt.Println("Buckets:")
22
23 for _, b := range result.Buckets {
24 fmt.Printf("* %s created on %s\n",
25 aws.StringValue(b.Name), aws.TimeValue(b.CreationDate))
26 }
27
28 for _, b := range result.Buckets {
29 fmt.Printf("%s\n", aws.StringValue(b.Name))
30 }
31
32}
33
列出bucket中的文件/文件夹
查看某个bucket中包含的文件/文件夹
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 1package main
2import (
3 "github.com/aws/aws-sdk-go/aws"
4 "github.com/aws/aws-sdk-go/aws/session"
5 "github.com/aws/aws-sdk-go/aws/credentials"
6 "github.com/aws/aws-sdk-go/service/s3"
7 "fmt"
8 "os"
9)
10
11func exitErrorf(msg string, args ...interface{}) {
12 fmt.Fprintf(os.Stderr, msg+"\n", args...)
13 os.Exit(1)
14}
15
16func main() {
17
18 配置同上
19
20 // bucket后跟,go run ....go bucketname
21 bucket := os.Args[1]
22 fmt.Printf(bucket)
23 fmt.Printf("\n")
24
25 svc := s3.New(sess)
26
27 params := &s3.ListObjectsInput{
28 Bucket: aws.String(bucket),
29 }
30 resp, err := svc.ListObjects(params)
31
32 if err != nil {
33 exitErrorf("Unable to list items in bucket %q, %v", bucket, err)
34 }
35
36 for _, item := range resp.Contents {
37 fmt.Println("Name: ", *item.Key)
38 fmt.Println("Last modified:", *item.LastModified)
39 fmt.Println("Size: ", *item.Size)
40 fmt.Println("Storage class:", *item.StorageClass)
41 fmt.Println("")
42 }
43
44}
45
bucket的创建
创建bucket
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 1package main
2
3import (
4 导包同上
5)
6
7
8func exitErrorf(msg string, args ...interface{}) {
9 fmt.Fprintf(os.Stderr, msg+"\n", args...)
10 os.Exit(1)
11}
12
13func main() {
14
15 配置同上
16
17 bucket := os.Args[1]
18
19 if len(os.Args) != 2 {
20 exitErrorf("Bucket name required\nUsage: %s bucket_name",
21 os.Args[0])
22 }
23
24
25 // Create S3 service client
26 svc := s3.New(sess)
27
28 params := &s3.CreateBucketInput{
29 Bucket: aws.String(bucket),
30 }
31
32 _, err = svc.CreateBucket(params)
33
34 if err != nil {
35 exitErrorf("Unable to create bucket %q, %v", bucket, err)
36 }
37
38 // Wait until bucket is created before finishing
39 fmt.Printf("Waiting for bucket %q to be created...\n", bucket)
40
41 err = svc.WaitUntilBucketExists(&s3.HeadBucketInput{
42 Bucket: aws.String(bucket),
43 })
44
45 if err != nil {
46 exitErrorf("Error occurred while waiting for bucket to be created, %v", bucket)
47 }
48
49 fmt.Printf("Bucket %q successfully created\n", bucket)
50}
51
bucket的文件上传
往某个固定的bucket里传文件
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 1package main
2
3import (
4 "github.com/aws/aws-sdk-go/aws"
5 "github.com/aws/aws-sdk-go/aws/session"
6 "github.com/aws/aws-sdk-go/aws/credentials"
7 "github.com/aws/aws-sdk-go/service/s3/s3manager"
8 "fmt"
9 "os"
10)
11
12
13func exitErrorf(msg string, args ...interface{}) {
14 fmt.Fprintf(os.Stderr, msg+"\n", args...)
15 os.Exit(1)
16}
17
18func main() {
19
20 配置同上
21
22 if len(os.Args) != 3 {
23 exitErrorf("bucket and file name required\nUsage: %s bucket_name filename",
24 os.Args[0])
25 }
26
27 bucket := os.Args[1]
28 filename := os.Args[2]
29
30 file, err := os.Open(filename)
31 if err != nil {
32 exitErrorf("Unable to open file %q, %v", err)
33 }
34
35 defer file.Close()
36
37 uploader := s3manager.NewUploader(sess)
38
39 _, err = uploader.Upload(&s3manager.UploadInput{
40 Bucket: aws.String(bucket),
41 Key: aws.String(filename),
42 Body: file,
43 })
44 if err != nil {
45 // Print the error and exit.
46 exitErrorf("Unable to upload %q to %q, %v", filename, bucket, err)
47 }
48
49 fmt.Printf("Successfully uploaded %q to %q\n", filename, bucket)
50}
51
bucket的文件下载
下载某个bucket中的某个文件
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 1package main
2
3import (
4 "github.com/aws/aws-sdk-go/aws"
5 "github.com/aws/aws-sdk-go/aws/session"
6 "github.com/aws/aws-sdk-go/aws/credentials"
7 "github.com/aws/aws-sdk-go/service/s3"
8 "github.com/aws/aws-sdk-go/service/s3/s3manager"
9
10 "fmt"
11 "os"
12)
13
14
15func exitErrorf(msg string, args ...interface{}) {
16 fmt.Fprintf(os.Stderr, msg+"\n", args...)
17 os.Exit(1)
18}
19
20func main() {
21
22 配置同上
23
24 if len(os.Args) != 3 {
25 exitErrorf("Bucket and item names required\nUsage: %s bucket_name item_name",
26 os.Args[0])
27 }
28
29 bucket := os.Args[1]
30 item := os.Args[2]
31
32 file, err := os.Create(item)
33 if err != nil {
34 exitErrorf("Unable to open file %q, %v", err)
35 }
36
37 defer file.Close()
38
39 downloader := s3manager.NewDownloader(sess)
40
41 numBytes, err := downloader.Download(file,
42 &s3.GetObjectInput{
43 Bucket: aws.String(bucket),
44 Key: aws.String(item),
45 })
46if err != nil {
47 exitErrorf("Unable to download item %q, %v", item, err)
48}
49
50fmt.Println("Downloaded", file.Name(), numBytes, "bytes")
51}
52
bucket的文件删除
删除某个bucket里面的某个文件
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 1package main
2
3import (
4 "github.com/aws/aws-sdk-go/aws"
5 "github.com/aws/aws-sdk-go/aws/session"
6 "github.com/aws/aws-sdk-go/aws/credentials"
7 "github.com/aws/aws-sdk-go/service/s3"
8 "fmt"
9 "os"
10)
11
12
13func exitErrorf(msg string, args ...interface{}) {
14 fmt.Fprintf(os.Stderr, msg+"\n", args...)
15 os.Exit(1)
16}
17
18func main() {
19
20 配置同上
21
22 if len(os.Args) != 3 {
23 exitErrorf("Bucket and object name required\nUsage: %s bucket_name object_name",
24 os.Args[0])
25 }
26
27 bucket := os.Args[1]
28 obj := os.Args[2]
29
30 svc := s3.New(sess)
31
32 _, err = svc.DeleteObject(&s3.DeleteObjectInput{Bucket: aws.String(bucket), Key: aws.String(obj)})
33 if err != nil {
34 exitErrorf("Unable to delete object %q from bucket %q, %v", obj, bucket, err)
35 }
36
37 err = svc.WaitUntilObjectNotExists(&s3.HeadObjectInput{
38 Bucket: aws.String(bucket),
39 Key: aws.String(obj),
40 })
41
42 fmt.Printf("Object %q successfully deleted\n", obj)
43}
44
代码所在地
https://github.com/Alexanderklau/Go_poject/tree/master/Go-Storage