Skip to content

Instantly share code, notes, and snippets.

@na0AaooQ
Last active February 13, 2019 14:05
Show Gist options
  • Save na0AaooQ/5e2e74aa036493a019e9 to your computer and use it in GitHub Desktop.
Save na0AaooQ/5e2e74aa036493a019e9 to your computer and use it in GitHub Desktop.
AWS S3上に保存されているファイルの更新タイムスタンプチェック ref: https://qiita.com/na0AaooQ/items/a6a209742bbe073ed12e
$ aws s3 sync s3://test-web-data-store/test/ /home/ec2-user/
download: s3://test-web-data-store/test/test2.txt to ./test2.txt
download: s3://test-web-data-store/test/test.txt to ./test.txt
$
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListAllMyBuckets"],
"Resource": "arn:aws:s3:::*"
},
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetBucketLocation"
],
"Resource": "arn:aws:s3:::test-web-data-store"
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": "arn:aws:s3:::test-web-data-store/*"
}
]
}
$ date
Sun Dec 21 17:13:13 UTC 2014
$ aws s3 ls s3://test-web-data-store/test/
2014-12-21 16:53:39 0
$
$ date > /tmp/test.txt
$
$ aws s3 cp /tmp/test.txt s3://test-web-data-store/test/
upload: ../../tmp/test.txt to s3://test-web-data-store/test/test.txt
$
$ aws s3 ls s3://test-web-data-store/test/
2014-12-21 16:53:39 0
2014-12-21 17:14:05 29 test.txt
$
$ rm -f /tmp/test.txt
$ ll /tmp/test.txt
ls: cannot access /tmp/test.txt: No such file or directory
$
$ aws s3 ls s3://test-web-data-store/test/ | grep 'test.txt' | awk '{print $1,$2}'
2014-12-21 17:14:05
$
$ vi s3_download_test.sh
$ chmod 754 s3_download_test.sh
$ chmod 754 s3_download_test.sh
$ aws s3 ls s3://test-web-data-store/test/test.txt
2014-12-21 17:14:05 29 test.txt
$
$ ./s3_download_test.sh
not diff [s3://test-web-data-store/test//test.txt]
$
$ aws s3 ls s3://test-web-data-store/test/test.txt
2014-12-21 17:14:05 29 test.txt
$
$ ll /tmp/test.txt
ls: cannot access /tmp/test.txt: No such file or directory
$
$ date >> test.txt
$ date >> test.txt
$
$ aws s3 cp test.txt s3://test-web-data-store/test/
upload: ./test.txt to s3://test-web-data-store/test/test.txt
$
$ rm test.txt
$ ll test.txt
ls: cannot access test.txt: No such file or directory
$ ll /tmp/test.txt
ls: cannot access /tmp/test.txt: No such file or directory
$
$ aws s3 ls s3://test-web-data-store/test/test.txt
2014-12-21 17:37:49 58 test.txt
$
$ ./s3_download_test.sh
diff [s3://test-web-data-store/test/test.txt]
aws s3 cp s3://test-web-data-store/test/test.txt /tmp/
download: s3://test-web-data-store/test/test.txt to ../../tmp/test.txt
$
$ ll /tmp/test.txt
-rw-rw-r-- 1 ec2-user ec2-user 58 Dec 21 17:45 /tmp/test.txt
$
#!/bin/sh
S3_BUCKET="s3://test-web-data-store/test"
S3_CHECK_FILE="test.txt"
OLD_TIMESTAMP_FILE="/tmp/old_file_timestamp.txt"
NEW_TIMESTAMP_FILE="/tmp/new_file_timestamp.txt"
DOWNLOAD_DIR="/tmp"
if [ -f ${NEW_TIMESTAMP_FILE} ] ; then
mv ${NEW_TIMESTAMP_FILE} ${OLD_TIMESTAMP_FILE}
fi
# S3バケット上のファイルのタイムスタンプを取得
aws s3 ls ${S3_BUCKET}/ | grep "${S3_CHECK_FILE}" | awk '{print $1,$2}' > ${NEW_TIMESTAMP_FILE}
if [ -f ${OLD_TIMESTAMP_FILE} ] ; then
DIFF_COUNT=`diff ${NEW_TIMESTAMP_FILE} ${OLD_TIMESTAMP_FILE} | wc -l`
if [ ${DIFF_COUNT} -ge 1 ] ; then
echo "diff [${S3_BUCKET}/${S3_CHECK_FILE}]"
echo "aws s3 cp ${S3_BUCKET}/${S3_CHECK_FILE} ${DOWNLOAD_DIR}/"
aws s3 cp ${S3_BUCKET}/${S3_CHECK_FILE} ${DOWNLOAD_DIR}/
else
echo "not diff [${S3_BUCKET}/${S3_CHECK_FILE}]"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment