get last modified object from S3 CLI

后端 未结 5 1921
别跟我提以往
别跟我提以往 2020-12-04 09:49

I have a use case where I programmatically bring up an EC2 instance, copy and executable file from S3, run it and shut down the instance (done in user-data). I need to get o

5条回答
  •  囚心锁ツ
    2020-12-04 10:18

    Following is bash script, that downloads latest file from a S3 Bucket. I used AWS S3 Synch command instead, so that it would not download the file from S3 if already existing.

    --exclude, excludes all the files

    --include, includes all the files matching the pattern

    #!/usr/bin/env bash
    
        BUCKET="s3://my-s3-bucket-eu-west-1/list/"
        FILE_NAME=`aws s3 ls $BUCKET  | sort | tail -n 1 | awk '{print $4}'`
        TARGET_FILE_PATH=target/datdump/
        TARGET_FILE=${TARGET_FILE_PATH}localData.json.gz
    
        echo $FILE_NAME
        echo $TARGET_FILE
    
        aws s3 sync $BUCKET $TARGET_FILE_PATH --exclude "*" --include "*$FILE_NAME*"
    
        cp target/datdump/$FILE_NAME $TARGET_FILE
    

    p.s. Thanks @David Murray

提交回复
热议问题