Last active
November 9, 2015 06:21
-
-
Save mrkn/240cb091396014bd501e to your computer and use it in GitHub Desktop.
Python で S3 に保存されているファイルの種類を調べる方法 ref: http://qiita.com/mrkn/items/32c341972e10194b9619
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ brew install libmagic | |
$ pip install python-magic | |
$ pip install boto3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 対象ファイルのパスのプレフィックス | |
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