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 S3を使って、動画アップロード機能をつけてみた

0
Last updated at Posted at 2026-04-05

AWS S3を使って、動画アップロード機能をつけてみた

つけたい機能

フロントエンドからユーザが動画をアップロードして、我々がそれを見れるようにする。

課題

  • 普通のバックエンドを用意するとコストがかかる
  • 現在のRenderでのstatic siteの無料デプロイをそのまま使いたい

解決法

AWS S3の署名付きURLで動画をアップロードする

処理の流れ

  1. S3の署名付きURLを発行するAPI(Lambda)をたたく
  2. APIからURLを取得
  3. URLを使ってS3のバゲットに動画を送る

APIの仕様

エンドポイント

POST lambdaの関数URL

アクセス制御

  • 認証・認可: なし(パブリックアクセス可能)

リクエスト

リクエストヘッダー
Key Value 必須 備考
Content-Type application/json 必須
Origin <許可されたフロントエンドのドメイン> 必須 ブラウザにより自動付与
リクエストボディ (JSON)
フィールド名 必須 説明
fileName string 必須 アップロード対象の元のファイル名(拡張子含む)

リクエスト例:

{
  "fileName": "example_image.png"
}

レスポンス

成功200
{
  "presignedUrl": "thisispresigneurl",
  "fileKey": "uploads/123e4567-e89b-12d3-a456-426614174000_example_image.png"
}
失敗400
{
  "error": "fileName is required"
}
失敗500
{
  "error": "Internal Server Error"
}

AWS

自分がAWSについて整理したメモ

署名付きURL

認証情報+以下の4つが取得するためには必要。

Amazon S3 バケット

オブジェクトキー (オブジェクトのダウンロード先は Amazon S3 バケット、アップロード先はアップロード先のファイル名)

HTTP メソッド (オブジェクトのダウンロードの GET、アップロードの PUT、オブジェクトメタデータの読み取りの HEAD など)

有効期限の時間間隔

AWS Lambda

選定理由

  • URL発行はセキュリティ(アクセスキーの管理)によりバックエンドでおこなう必要あり => セキュリティ
  • 認証情報が漏れないセキュリティ高い(AWS内部のため) => セキュリティ
  • 1か月あたり100万件の無料枠 => コスパ

設定など

CORSを設定。

origin: origin
header: Content-Type, Origin
method: POST

lambdaにロールを設定: s3:PutObject

ファイルのアップロード

署名付きURLでバゲットに送る。urlと中身だけで送れる。

curlコマンド
curl -X PUT -T "path/to/your/local/file" -H "Content-Type: application/octet-stream" "generated-presigned-url"

S3の設定

CORS

origin: origin
header: content-type
method: PUT
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?