1
0

More than 3 years have passed since last update.

小技: S3 のバケットの中のファイルを一斉書き換え

Posted at

S3 の間でファイルを一斉に移動させた時などに ACL で public read が付与されていなかったり、SVG ファイルの metadata がなぜか binary/octat-stream になってて image/svg+xml でなかったりしました。

なので、シェルスクリプトで一括で変換できるスクリプトを作りました。

PREFIX で、特定のフォルダ (Key) のみに限定して実行させることもできるようにしました。

特定 Bucket の prefix に ACL public read を加える

シェル

s3-bucket-acl.sh
# /bin/bash
BUCKET=$1
PREFIX=$2
for KEY in $(aws s3api list-objects \
  --bucket ${BUCKET} | jq -r '.Contents[].Key')
  --prefix ${PREFIX}\
do
  aws s3api put-object-acl \
    --bucket ${BUCKET} \
    --key ${KEY} \
    --acl public-read
  echo "${BUCKET}/${KEY}"
done

実行例

sh s3-bucket-acl.sh [バケット名] [プリフィックス]

SVG 形式のファイルに正しい image/svg+xml の content-type を付与する

s3-bucket-svg.sh
# /bin/bash
BUCKET=$1
PREFIX=$2
for KEY in $(aws s3api list-objects \
    --bucket ${BUCKET}\
    --query "Contents[].[Key]" \
    --output text | grep "\.svg$")
    do
    aws s3api copy-object \
    --bucket ${BUCKET} \
    --copy-source ${BUCKET}/${KEY} \
    --key ${KEY} \
    --metadata-directive "REPLACE" \
    --content-type "image/svg+xml"
    echo "${BUCKET}/${KEY}"
    done

実行例

sh s3-bucket-svg.sh [バケット名] [プリフィックス]
1
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
1
0