23
25

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 3 years have passed since last update.

AWS LambdaでS3からHTMLファイルを取得して返す

Last updated at Posted at 2021-07-05

色々と応用できそうなので残しておきます。

環境

Python3.7
API Gateway プロキシ統合あり

処理

import boto3
import os

# バケット名は環境変数から取得
BUCKET_NAME = os.environ['BUCKET_NAME']
s3 = boto3.resource('s3')
filepath = '/tmp/hogehoge.html'

def main(event, context):
    
    bucket = s3.Bucket(BUCKET_NAME)
    bucket.download_file('hogehoge.html', filepath)
    
    with open(filepath , encoding='utf-8') as f:
        html = f.read()
    
    return {
        "statusCode": 200,
        "headers": {"Content-Type": "text/html"},
        "body": html
    }
23
25
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
23
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?