LoginSignup
1
0

More than 3 years have passed since last update.

【Cloud Functions】Storageに置かれたGZIPファイルを自動解凍

Posted at

前説

ソース

先人が落としてくれたZipファイルを解凍するコード(参考リンク-3)を改造しました。
Cloud Storageトリガーで起動するCloud Functions(Python 3.7)で、単一ファイルをGzip圧縮している前提です。

main.py
from google.cloud import storage
import io,gzip

def hello_gcs(event, context):

    client = storage.Client()
    bucket_name = event['bucket']
    bucket = client.get_bucket(bucket_name)
    blob_name = event['name']
    blob = bucket.blob(blob_name)

    data = io.BytesIO(blob.download_as_string())
    with gzip.open(data) as gz:
        file = gz.read()
        blob_decompress = bucket.blob(blob_name.replace('.gz',''))
        blob_decompress.upload_from_string(file)

大きな問題点

  • Cloud Functionsに割り当てられる最大メモリは2GB(投稿時点で)
  • 処理がメモリの容量を超えると当然エラーになる

⇒ 置かれるGZIPファイルの時点で容量が2GB以上になる可能性があったので、結局ボツになりました。。

参考リンク

  1. Cloud Functions ガイド:Google Cloud Storage トリガー
  2. GCS にファイルが配置されたらイベント駆動で BigQuery にデータロードするサーバレスなジョブをつくってみた
  3. Googleクラウドストレージで.Zipファイルを解凍するにはどうすればよいですか?
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