17
15

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]API Gateway + S3でファイルをダウンロードさせる

Last updated at Posted at 2016-01-26

やりたいこと

  • API GatewayのInvoke URLを叩くとS3においてあるファイルがダウンロードされる。

まずやってみたこと(失敗)

Integration type」でHTTP Proxyを選択して「Endpoint URL」にS3のStatic Website HostingのEndpointを指定すれば行けると思ったのですがInternal server Errorとなりました。

うまくいったやり方

ひとまずうまくいったやり方が以下になります。

S3にバケットを作成

任意のバケットを作成し、ダウンロードさせたいファイルを配置します。

S3アクセス用のroleを作成

IAMでAPI GatewayからS3の該当バケットにアクセスするためのroleを作成します。

API Gatewayの設定

以下のように設定します。

Integration Request

Integration Type
	AWS Service Proxyを選択
AWS Region
	S3のリージョンを選択
AWS Service
	S3を選択
HTTP method
	任意
Path override
	S3のkey(bucketname含む)を入力
Execution role
	作成したroleのarnを入力

Method Response

Response Models for 200
	Content type
		application/octet-stream

課題

Content-Disposition: attachment; filename="filename"
の設定ができていません。

Method Response」の「Response Headers for 200」に
Content-Disposition
を追加して、
Integration Response」の「Header Mappings」の「Mapping value」に
attachment; filename="filename"
を指定すると以下のエラーとなりました。

Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: attachment; filename="filename"]

ここは現在調査中です。

2016/02/09追記

2016/02/09現在、「Method Execution」からのTESTを実行するとInternal server errorとなります。
どうやらAPI Gatewayのバグの用で、Deployすると正常に動作します。

17
15
1

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
17
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?