LoginSignup
6

More than 5 years have passed since last update.

posted at

updated at

Organization

s3cmd で日本語ファイルを ダウンロード、アップロードしたい

s3cmd で 日本語が含まれるディレクトリ、ファイルをアップロード、ダウンロードしようとすると、

↓のようなエラーがでます。

[root@localhost ~]# s3cmd --config=/s3cmd/.s3cfg get s3://meister-staging-webroot-published/Account/1/footer_image2/フッタ.gif /tmp/

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    An unexpected error has occurred.
  Please report the following lines to:
   s3tools-bugs@lists.sourceforge.net
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Problem: UnicodeEncodeError: 'ascii' codec can't encode characters in position 63-65: ordinal not in range(128)
S3cmd:   1.0.0

Traceback (most recent call last):
  File "/usr/bin/s3cmd", line 2006, in <module>
    main()
  File "/usr/bin/s3cmd", line 1950, in main
    cmd_func(args)
  File "/usr/bin/s3cmd", line 439, in cmd_object_get
    remote_list = fetch_remote_list(args, require_attribs = False)
  File "/usr/bin/s3cmd", line 281, in fetch_remote_list
    uri_str = str(uri)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 63-65: ordinal not in range(128)

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    An unexpected error has occurred.
    Please report the above lines to:
   s3tools-bugs@lists.sourceforge.net
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

これは、

  • s3cmd → Pythonで書かれている
  • Pythonのデフォルトエンコーディング → ASCII

というのが原因の模様。

なので以下のような作業を行い、UTF-8 を扱えるようにすれば、日本語のファイルを扱うことができます。

[root@localhost ~]# cp -ra /usr/bin/s3cmd /usr/bin/s3cmd.orig
[root@localhost ~]# vim /usr/bin/s3cmd
#!/usr/bin/python

## Amazon S3 manager
## Author: Michal Ludvig <michal@logix.cz>
##         http://www.logix.cz/michal
## License: GPL Version 2

import sys
reload(sys)
sys.setdefaultencoding("UTF-8")
[root@localhost ~]# diff /usr/bin/s3cmd /usr/bin/s3cmd.orig
9,10d8
< reload(sys)
< sys.setdefaultencoding("UTF-8")

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
What you can do with signing up
6