Golang获取AWS S3桶文件

需求

工作需要,要用Golang写一个下载带有鉴权的aws s3桶文件的脚本
整体代码参考AWS官方的样例,由于官方样例是读取~/.aws/credentials的配置信息,修改手动指定AK SK

功能

下载带有鉴权的S3桶文件
注:本地路径如果包含不存在的文件夹会报错

用法

go run s3_download_object.go S3文件路径 本地路径
例如:
go run s3_download_object.go file/001.img a.img

代码

先安装AWS SDK
go get github.com/aws/aws-sdk-go

s3_download_object.go

package main

import (
	"fmt"
	"os"

	"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"
)

// Downloads an item from an S3 Bucket in the region configured in the shared config
// or AWS_REGION environment variable.
//
// Usage:
//
//	go run s3_download_object.go S3_ITEM ITEM
func main() {
	if len(os.Args) != 3 {
		exitErrorf("Command Error: s3_download_object S3_ITEM ITEM",
			os.Args[0])
	}
	if len(os.Args) == 3 {
		item := os.Args[1]
		file_item := os.Args[2]
		snap_file := file_item + "_snap"
		bucket := "XXXXXXXX"      // bucket名称

		access_key := "XXXXXXX"   // aws AK
		secret_key := "XXXXXXXXXXXXXX"  // aws SK

		sess, err := session.NewSession(&aws.Config{
			Credentials:      credentials.NewStaticCredentials(access_key, secret_key, ""),
			Region:           aws.String("XXXXXXX"),   // S3桶区域
			DisableSSL:       aws.Bool(false),
			S3ForcePathStyle: aws.Bool(false),
		})

		downloader := s3manager.NewDownloader(sess)
		file, err := os.Create(snap_file)
		if err != nil {
			exitErrorf("Create Error!!!")
		}
		if err == nil {

			defer file.Close()
			_, err := downloader.Download(file,
				&s3.GetObjectInput{
					Bucket: aws.String(bucket),
					Key:    aws.String(item),
				})
			if err != nil {
				os.Remove(snap_file)

				exitErrorf("Download Error!!!")
			}
			if err == nil {
				os.Rename(snap_file, file_item)
				fmt.Println("Download Done!!!")
			}
		}
	}

}

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

Q.E.D.


一名北漂的网安工程师,希望这次能好好工作,不被毕业吧…