LoginSignup
7
0

More than 1 year has passed since last update.

事象 : S3に格納されている「!」がファイル名にあるファイルをダウンロードできない

$ aws s3 ls s3://sample-bucket-name/dir-path/ --profile chovin
2022-11-29 09:31:46      34304 file_name!.xls

$ aws s3 cp s3://sample-bucket-name/dir-path/file_name!.xls ~/Downloads/ --profile chovin
bash: !.xls: event not found

原因 : Bashのヒストリ展開が行われているから

!
履歴置換を開始します。ただし、 ブランク文字、 改行文字、 = 、 ( (シェルオプション extglob が組み込みコマンド shopt によって有効になっている場合) のいずれかが後に続く場合は除きます。
Man page of BASH

  1. 「!.xls」でヒストリ展開が行われる
  2. 「.xls」で始まるコマンドで実行された履歴を探す
  3. ないからエラー

参考 : [Bash] ダブルクォートで囲んだコマンド置換中で!を使うとエラーになる(例: echo "$(echo '!')") - Qiita

対応 : 「'」だけで囲う

# 成功パターン : パス全部を「'」で囲う
$ aws s3 cp 's3://sample-bucket-name/dir-path/file_name!.xls' ~/Downloads/ --profile chovin
download: s3://sample-bucket-name/dir-path/file_name!.xls to C:\Users\chovin\Downloads\file_name!.xls

# 成功パターン : ファイル名を「'」で囲う
$ aws s3 cp s3://sample-bucket-name/dir-path/'file_name!.xls' ~/Downloads/ --profile chovin
download: s3://sample-bucket-name/dir-path/file_name!.xls to C:\Users\chovin\Downloads\file_name!.xls

# 失敗パターン : パス全部を「"」で囲う
$ aws s3 cp "s3://sample-bucket-name/dir-path/file_name!.xls" ~/Downloads/ --profile chovin
bash: !.xls: event not found

# 失敗パターン : ファイル名を「"」で囲う
$ aws s3 cp s3://sample-bucket-name/dir-path/"file_name!.xls" ~/Downloads/ --profile chovin
bash: !.xls: event not found

# 失敗パターン : ファイル名を「'」と「"」で囲う
$ aws s3 cp s3://sample-bucket-name/dir-path/'"file_name!.xls"' ~/Downloads/ --profile chovin
fatal error: An error occurred (404) when calling the HeadObject operation: Key "dir-path/"file_name!.xls"" does not exist

# 失敗パターン : パス全部を「'」で囲う and ファイル名を「"」で囲う
$ aws s3 cp 's3://sample-bucket-name/dir-path/"file_name!.xls"' ~/Downloads/ --profile chovin
fatal error: An error occurred (404) when calling the HeadObject operation: Key "dir-path/"file_name!.xls"" does not exist
7
0
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
7
0