LoginSignup
17
11

More than 5 years have passed since last update.

aws-cliの結果をリダイレクト/パイプしようと思ったときに文字化けするときの対策

Last updated at Posted at 2016-06-24

こんな感じで、aws cliからコマンドをうつと、こんな感じになります(コマンド、出力はダミーです)。

$ aws s3 ls --recursive bucket/photo/1/
2014-05-06 16:45:30      11979 photo/1/20140506164529_スクリーンショット 2014-01-28 15.27.48.png

ところが、これにたとえば以下のようなコマンドに変えると、とたんに面倒くさいことになります。

$ aws s3 ls --recursive bucket/photo/1/ | tee /dev/null
2014-05-06 16:45:30      11979 photo/1/20140506164529_????????? 2014-01-28 15.27.48.pn

化けた!

なお、リダイレクトでファイル出力したときにも同じ現象が発生します。

対策

原因はawsコマンドを実装しているPython処理系にあります。Pythonは出力先に応じて文字コードを合わせてくれるのですが、リダイレクトやパイプの場合には、出力先の文字コードがわからないので、強制的にASCIIではき出してくれるのです!!! 余計なまねを!!!

対策としては、PYTHONIOENCODINGという環境変数・シェル変数を定義しておけばいいです。

$ PYTHONIOENCODING=UTF-8 aws s3 ls --recursive bucket/photo/1/ | tee /dev/null
2014-05-06 16:45:30      11979 photo/1/20140506164529_スクリーンショット 2014-01-28 15.27.48.png
17
11
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
17
11