Skip to content

Instantly share code, notes, and snippets.

@mrkn
Last active November 9, 2015 06:21
Show Gist options
  • Save mrkn/240cb091396014bd501e to your computer and use it in GitHub Desktop.
Save mrkn/240cb091396014bd501e to your computer and use it in GitHub Desktop.
Python で S3 に保存されているファイルの種類を調べる方法 ref: http://qiita.com/mrkn/items/32c341972e10194b9619
$ brew install libmagic
$ pip install python-magic
$ pip install boto3
# 対象ファイルのパスのプレフィックス
key_prefix = 'uploads/'
# 対象ファイル名のパターン
key_pattern = 'uploads/\d+/images/.*'
# ダウンロード用の一時ファイル名
temporary_filename = '/tmp/downloaded_file'
for s3_object in bucket.objects.filter(Prefix=key_prefix):
if not re.match(key_pattern, s3_object.key):
continue
s3_object.Object().download_file(temporary_filename)
mime_type = magic.from_file(temporary_filename, mime=True)
print("{}: {}".format(s3_object.key, mime_type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment