LoginSignup
12
13

More than 5 years have passed since last update.

AWS S3にアップロードしたファイルのContent-Typeを一括で書き換えるワンライナー

Posted at

問題

API 経由でのファイルアップロード処理の実装にミスるなどして、ファイルの Content-Type が binary/octet-stream になってしまって困ることがある。(あまりない)

解決策

aws cli を使ったワンライナーで以下のように書ける。
以下は、.jpg なファイル名のファイルに対して、image/jpeg を設定して回るサンプル。

$ aws s3 ls --recursive s3://<bucket_name>/<somewhere> | grep -e '.jpg$' | awk '{ print "s3://<bucket_name>/"$4 }' | while read origname; do aws s3 mv ${origname} ${origname}.bak; aws s3 mv ${origname}.bak ${origname} --metadata-directive REPLACE --content-type "image/jpeg"; done
  • s3 mv はファイル名の変更を伴わずに Content-Type のみを書き換えることはできないぽい( Cannot mv a file onto itself と怒られる)ので、一旦別のファイル名にして戻す
  • Content-Type を書き換えるときは --metadata-directive REPLACE オプションを指定する

参考

12
13
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
12
13