Quantcast
Channel: プログラミング
Viewing all articles
Browse latest Browse all 8275

【AWS】Amazon S3 ~ AWS CLI / aws s3 ls ~ - プログラム の超個人的なメモ

$
0
0

■ はじめに

https://dk521123.hatenablog.com/entry/2017/04/01/235355

の続き。

作業で、S3バケット内のファイル容量を調べる必要が出てきて
そのファイルの時間推移も併せて調べることとなった。
ちまちまManagement Console上から手作業で調べていたのだが
シェルでやった方が正確かつ後々同じことしそうなので記録しておく

目次

【1】aws s3 ls
 1)構文
 2) コマンド例
【2】オプション
 1)--recursive
 2)--summarize (--sum)
 3)--human-readable(--human)
【3】シェルスクリプトで半自動化
 1)s3バケット構成
 2)サンプル
 3)Tips

【1】aws s3 ls

* S3の一覧表示

https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/ls.html

1)構文

aws s3 ls(s3://【S3のバケット名】)

2) コマンド例

例1:s3バケット一覧を表示する

$ aws s3 ls

例2:容量を出力する
https://dev.classmethod.jp/articles/aws-cli-count-s3-filesize/

# --summarize (--sum):合計サイズとオブジェクトの数を表示# --recursive:再帰的に下位階層にあるパスのファイルまで対象にする
$ aws s3 ls s3://your-bucket/your-dir/  --recursive--sum
2024-10-02 00:00:00  3123232 your-dir/2024/10/02/test.csv

Total Object: 1
  Total Size: 3123232

【2】オプション

1)--recursive

* 再帰的に下位階層にあるパスのファイルまで対象にする

2)--summarize (--sum)

* 合計サイズとオブジェクトの数を表示

3)--human-readable(--human)

* ファイルサイズに単位をつけて表示

【3】シェルスクリプトで半自動化

1)s3バケット構成

s3://your-s3-bucket-name/
 /xxxx/data-type(e.g. customer)/yyyy(e.g. 2024)/MM (e.g. 01)//dd (e.g. 01)/ <= ここの部分の容量を取得したい
  <File1>
  ...
  <FileX>

2)サンプル

#!/bin/bashS3_BUCKET_NAME=your-s3-bucket-name
S3_ROOT_PATH=xxxx/customer/
OUTPUT_FILE="./customer.tsv"YEAR_LIST=(20232024)MONTH_LIST=(123456789101112)CURRENT_DATE=`date +"%Y%m%d"`# output the TSV headerecho -e "target_date\tfile_size">${OUTPUT_FILE}# YEAR partfor year in${YEAR_LIST[@]};
doecho"[DEBUG] target_year=$year"# MONTH partfor i in${MONTH_LIST[@]};domonth=`printf"%02d\n"${i}`echo"[DEBUG] target_month=$month"first_date=`date -d "-1 days + 1 month ${year}/${i}/1" +%Y%m01`end_day=`date +"%d" -d "-1 days + 1 month ${first_date}"`# DAY partfor((j=1; j<=end_day; j++))doday=`printf"%02d\n"${j}`target_date=${year}${month}${day}echo"target_date=${target_date}"if ["${target_date}"-gt"${CURRENT_DATE}"];thenecho"target_date [${target_date}] is greater than ${CURRENT_DATE}"breakfi######################### Main process######################### Run aws s3 ls to get file sizeresult=`aws s3 ls s3://${S3_BUCKET_NAME}/${S3_ROOT_PATH}${year}/${month}/${day}/ --recursive --sum |grep"Total Size:"`# Get file sizefile_size=`echo${result}| cut -d ':' -f 2|sed's/^ *\| *$//'`# Output into TSV fileecho -e "${target_date}\t${file_size}">>${OUTPUT_FILE}doneif ["${target_date}"-gt"${CURRENT_DATE}"];thenbreak;fidoneif ["${target_date}"-gt"${CURRENT_DATE}"];thenbreak;fidoneecho"Done"

3)Tips

* aws s3 ls のTipsではなく、サンプルで作成したBashのTips。

[a] 指定した年月で末日を算出する

date +%Y%m%d -d"-1 days + 1 month $(date -d '-1days + 1 month <年>/<月>'/1 +%Y%m01)"

https://orebibou.com/ja/home/201606/20160617_002/

関連記事

Amazon S3~ 入門編 ~
https://dk521123.hatenablog.com/entry/2017/03/06/212734
Amazon S3aws s3api ~
https://dk521123.hatenablog.com/entry/2024/05/22/235922
Amazon S3AWSCLI
https://dk521123.hatenablog.com/entry/2017/04/01/235355
Amazon S3AWSCLI / aws s3 sync ~
https://dk521123.hatenablog.com/entry/2024/06/06/203209
Amazon S3~ Boto3編 ~
https://dk521123.hatenablog.com/entry/2019/10/21/230004
シェル ~ 基本編・ループ while / for etc ~
https://dk521123.hatenablog.com/entry/2021/08/09/000000


Viewing all articles
Browse latest Browse all 8275

Trending Articles