1
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?

More than 5 years have passed since last update.

AWS S3 New()で困ったお話し

1
Posted at

S3 Selectしたい要件があったので実装してみたが、
動く環境によって実装方法が違ったというお話。

ちゃんと公式ドキュメント読めば予め回避できたかもしれない。。

前提条件として、S3Bucketには適切なアクセス権限がついているものとする。
※EC2ロールの許可

だめパターン(roleARN指定)

EC2と同じroleでNew()すればよいやろ。

sess := session.Must(session.NewSession())
creds := stscreds.NewCredentials(sess, arn)
S3: s3.New(
	sess,
	aws.NewConfig().WithRegion(region).WithCredentials(creds),
)

EC2にあげて、さぁテスト。

AccessDenied: Access denied
    status code: 403

なんでや...

ユーザ指定

こうならlocalでもどこでも動くけど、
S3所有者だと権限強すぎだし、
別ユーザ作成するのもな。。。

sess := session.Must(session.NewSession())
conf := aws.NewConfig().WithRegion(region).WithCredentials(credentials.NewStaticCredentials([id], [secKey], ""))
S3: s3.New(
	sess,
	conf,
)

正解(EC2で実行しているのでsessionをshare)

こうだった。
※EC2実行ロールでS3アクセスできる前提。

sess := session.Must(session.NewSessionWithOptions(session.Options{
	SharedConfigState: session.SharedConfigEnable,
}))
S3: s3.New(
	sess,
	&aws.Config{Region: aws.String(region)},
)

参考:https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/go/example_code/s3/s3.go#L44

1
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
1
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?