0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

aws-sdk-goの紹介

Goでs3を扱う方法を紹介します。
awsコマンドは有効化済みの想定です

インストール

go get github.com/aws/aws-sdk-go/aws
go get github.com/aws/aws-sdk-go/aws/session
go get github.com/aws/aws-sdk-go/service/s3

プログラム本体

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
)

func main() {
	sess, err := session.NewSession(&aws.Config{
		Region: aws.String("ap-northeast-1")},
	)
	if err != nil {
		log.Fatal(err)
	}

	svc := s3.New(sess)

	// バケット一覧出力
	result, err := svc.ListBuckets(nil)
	if err != nil {
		log.Fatal(err)
	}
	for _, bucket := range result.Buckets {
		fmt.Printf("%s\n", aws.StringValue(bucket.Name))
	}

	// ファイルアップロード
	file, err := os.Open("file_name")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	_, err = svc.PutObject(&s3.PutObjectInput{
		Bucket: aws.String("bucket_name"),
		Key:    aws.String("file_name"),
		Body:   file,
	})

	if err != nil {
		log.Fatal(err)
	}
}

特別複雑なことは無く、他の言語でawsを操作したことがある人なら難なく扱えると思います。
Goでこういうことをしようとするともっと面倒なのかと思っていましたが、案外シンプルに収まったので、今後はこういう作業もGoでやってみようと思いました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?