0
1

github.com/aws/aws-sdk-go-v2のEndpointが非推奨になっていた件

Posted at

概要

新しいサービスを作るにあたってGo言語からR2に読み書きする必要があり、github.com/aws/aws-sdk-go-v2を使ったS3クライアントの初期化処理を既存サービスから流用しようとしました。その際、EndpointResolverWithOptionsFuncなどの関数がDeprecatedになっていました。

これについて言及している記事があまり無かったので、メモがてら記事にしてみました。

内容

調べたところSDKのaws/endpoints.go内の構造体や関数が非推奨になっていました。

EndpointResolverのインターフェース定義部分に以下のようなコメントがありました。

// Deprecated: The global endpoint resolution interface is deprecated. The API
// for endpoint resolution is now unique to each service and is set via the
// EndpointResolverV2 field on service client options. Setting a value for
// EndpointResolver on aws.Config or service client options will prevent you
// from using any endpoint-related service features released after the
// introduction of EndpointResolverV2. You may also encounter broken or
// unexpected behavior when using the old global interface with services that
// use many endpoint-related customizations such as S3.

翻訳すると

非推奨: グローバルエンドポイント解決インターフェイスは廃止予定です。エンドポイント解決のためのAPIは、現在では各サービスに固有であり、サービスクライアントオプションのEndpointResolverV2フィールドを介して設定されます。aws.ConfigまたはサービスクライアントオプションでEndpointResolverの値を設定すると、EndpointResolverV2の導入後にリリースされたエンドポイント関連のサービス機能を使用できなくなります。また、S3のようなエンドポイント関連のカスタマイズを多用するサービスで古いグローバルインターフェースを使用すると、壊れたり予期しない動作が発生する可能性があります。

つまり、グローバルエンドポイント設定から各サービスの設定に変更され、SDKの各サービスに定義されているEndpointResolverV2を使う必要があるらしいです。

そして、既存サービス側が使用しているバージョン(v1.24.0)においてもコメントでDeprecatedと書かれていました。

ちなみにR2のaws-go-sdkのドキュメントは更新されておらず、issue #12043が立っていました。

従来実装

		akey := c.AccessKeyID
		skey := c.SecretAccessKey
		region := c.Region
		endpoint := c.Endpoint

		creds := credentials.NewStaticCredentialsProvider(akey, skey, "")
		resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
			return aws.Endpoint{
				URL:           endpoint,
				SigningRegion: region,
			}, nil
		})

		cfg, err := awsconfig.LoadDefaultConfig(
			context.TODO(),
			awsconfig.WithCredentialsProvider(creds),
			awsconfig.WithEndpointResolverWithOptions(resolver),
			awsconfig.WithRegion(region),
		)
		if err != nil {
			panic(err)
		}

		client := s3.NewFromConfig(cfg, func(o *s3.Options) {
			o.UsePathStyle = true
		})

新しい実装

aws.EndpointResolverWithOptionsFuncでresolverを作り、awsのグローバル設定として設定する記述が要らなくなり、s3のサービスの設定として指定するようになります。

		akey := c.AccessKeyID
		skey := c.SecretAccessKey
		region := c.Region
		endpoint := c.Endpoint

		creds := credentials.NewStaticCredentialsProvider(akey, skey, "")

		cfg, err := awsconfig.LoadDefaultConfig(
			context.TODO(),
			awsconfig.WithCredentialsProvider(creds),
			awsconfig.WithRegion(region),
		)
		if err != nil {
			panic(err)
		}

		client := s3.NewFromConfig(cfg, func(o *s3.Options) {
			o.UsePathStyle = true
			o.BaseEndpoint = aws.String(endpoint)
		})

最後に

同じような内容で詰まっている方の助けになれば幸いです。
何か間違いや修正すべきところがあればコメントで教えてください。

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