Windows版 AWS CLI の UnicodeWarning の回避方法

2015.08.11

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Windows環境では AWS Tools for Windows PowerShell が提供されていますが、多くのプラットフォームOSで動作する AWS CLI をWindows環境でも利用したという要望は少なくありません。しかし、Windows版の AWS CLI (AWSCLI64.msi)をインストールして aws s3 ls s3://xxxxx/ 実行したところ、UnicodeWarning が表示される問題に直面しました。

以降では以下のBucketを作って検証しました。

(ルート)
 ├ root-folder/
 │  ├ folder-file-1
 │  └ folder-file-2
 ├ root-file-1
 └ root-file-2

Windows版の AWS CLI の導入 / UnicodeWarning

AWSの公式サイトの「AWS コマンドラインインターフェイス」 からWindowsの64ビット用インストーラ(AWSCLI64.msi)をダウンロードしてインストールします。

最初に初期設定を登録します。設定情報は、C:¥Users¥xxx¥.aws¥config に保存されます。

C:¥Users¥xxx> aws configure
AWS Access Key ID [None]: youraccesskey
AWS Secret Access Key [None]: yoursecretkey
Default region name [None]: ap-northeast-1
Default output format [None]: json

Windowsの64ビット用インストーラ(AWSCLI64.msi)をインストールすると AWS CLI と一緒にバンドルされたPythonがインストールされます。awsコマンドで確認します。

C:¥Users¥xxx>aws --version
aws-cli/1.7.44 Python/2.7.9 Windows/8

aws s3 ls s3://root-bucket-vt/ を実行すると、UnicodeWarning というエラーが表示されます。

C:¥Users¥xxx>aws s3 ls s3://root-bucket-vt/
 PRE root-folder/
C:¥Program Files¥Amazon¥AWSCLI¥.¥dateutil¥parser.py:428: UnicodeWarning: Unicode
 equal comparison failed to convert both arguments to Unicode - interpreting the
m as being unequal
2015-08-10 17:35:53 0 root-file-1
2015-08-10 17:35:53 0 root-file-2

C:¥Users¥xxx>aws s3 ls s3://root-bucket-vt/root-folder/
C:¥Program Files¥Amazon¥AWSCLI¥.¥dateutil¥parser.py:428: UnicodeWarning: Unicode
 equal comparison failed to convert both arguments to Unicode - interpreting the
m as being unequal
2015-08-10 17:37:25 0
2015-08-11 00:57:14 0 folder-file-1
2015-08-11 00:57:14 0 folder-file-2

どうやら、このエラーは、AWS CLI が使っている日時系ライブラリの Unicode 操作に起因する不具合のようです。

参考:s3 sync/ls issues UnicodeWarning in Windows environment(なんか見たことのある人のような...)

とりあえず、Windowsの64ビット用インストーラ(AWSCLI64.msi)はアンインストールします。設定情報 C:¥Users¥xxx¥.aws¥configはそのまま流用します。

解決方法0:タイムゾーンをJSTに設定する(2018/06/25追記)

まずは環境変数としてタイムゾーンの設定を試みてください。下記はJSTの設定例となります。改善しない場合は、、解決方法1や解決方法2をご検討ください。

  • PowerShellの場合
Set-Item env:tz -Value jst
  • コマンドプロンプトの場合
set tz=jst

解決方法1:Python2.7 の pip で AWS CLI をインストールする

Pythonの公式サイト から Python 2.7.10 をダウンロードしてインストールします。

pip で AWS CLI をインストールします。

C:¥Python27¥Scripts>pip install awscli

C:¥Python27¥Scripts¥aws.cmd の19行目を %PythonExe% -x %PythonExeFlags% "%~f0" %* から %PythonExe% -W ignore::UnicodeWarning -x %PythonExeFlags% "%~f0" %* に書き換えます。

@echo OFF
REM="""
setlocal
set PythonExe=""
set PythonExeFlags=

for %%i in (cmd bat exe) do (
 for %%j in (python.%%i) do (
 call :SetPythonExe "%%~$PATH:j"
 )
)
for /f "tokens=2 delims==" %%i in ('assoc .py') do (
 for /f "tokens=2 delims==" %%j in ('ftype %%i') do (
 for /f "tokens=1" %%k in ("%%j") do (
 call :SetPythonExe %%k
 )
 )
)
%PythonExe% -W ignore::UnicodeWarning -x %PythonExeFlags% "%~f0" %*
goto :EOF
     :
     :

UnicodeWarning が無事に表示されなくなりました。

C:¥Python27¥Scripts>aws s3 ls s3://root-bucket-vt
 PRE root-folder/
2015-08-10 17:35:53 0 root-file-1
2015-08-10 17:35:53 0 root-file-2

解決方法2:Python3.4 の pip で AWS CLI をインストールする

Pythonの公式サイト から Python 3.4.3 をダウンロードしてインストールします。

pip で AWS CLI をインストールします。

C:¥Python34¥Scripts>pip install awscli

Python3系では、UNICODEが関連の仕様が見直されたので、UnicodeWarning が表示されません。

C:¥Python27¥Scripts>aws s3 ls s3://root-bucket-vt
 PRE root-folder/
2015-08-10 17:35:53 0 root-file-1
2015-08-10 17:35:53 0 root-file-2

最後に

UnicodeWarningは警告メッセージなので深刻な問題にはなりませんが、お客様の導入環境でこのメッセージが標準エラー出力に表示されるのは、あまり望ましくありません。解決方法2は根本的な解決といえますが、執筆時点(2015/08)では、LinuxにインストールされているデフォルトPythonは2.x系なので、解決方法1を採用することが少なくないでしょう。 なお、Pythonの公式サイト からダウンロードしたPythonのインストーラーは、PATHを設定しないので、環境変数PATHに C:¥<Pythonのホーム>¥Scripts を追加することをお忘れず。