Skip to content

Instantly share code, notes, and snippets.

@polidog
Last active October 26, 2015 09:52
Show Gist options
  • Save polidog/a3ce02eabff329936269 to your computer and use it in GitHub Desktop.
Save polidog/a3ce02eabff329936269 to your computer and use it in GitHub Desktop.
うっかりAmazon 3SにNFD(utf-8-mac)なファイル名をアップロードしたときに修正する方法
# coding: utf-8
import re
import unicodedata
from sh import aws
BUCKET = "aws-bucket-name" # 対象のs3 bucket名
s3 = aws.bake("s3")
def _s3rename(path, name, rename):
namePath = "s3://"+BUCKET+path+name
renamePath = "s3://"+BUCKET+path+rename
# print("mv "+namePath+" " + renamePath)
result = s3("mv",namePath,renamePath)
print(result)
def s3rename(path):
for name in s3("ls","s3://"+BUCKET+path):
name = re.sub(r'\d+\-\d+\-\d+(\s+)\d+\:\d+\:\d+\s+\d+\s+','',name.rstrip());
rename = unicodedata.normalize("NFC",name)
if name != rename :
_s3rename(path,name,rename)
else:
print("non NFD:"+name)
## 実行例
s3rename("/hoge/fuga/")
@polidog
Copy link
Author

polidog commented Oct 4, 2014

使い方

必要なライブラリを用意

sudo pip install awscli
sudo pip install sh

AWSの設定

aws configure 

コマンドの実行

$ python ./gorilla3.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment