0

我有一个脚本可以将文件从目录上传到 s3 存储桶。

我的剧本是这样的

aws s3 sync <directory_of_files_to_upload> s3://<bucket-name>/

当我运行这个脚本时,整个文件都会正确上传。我想在上传新文件时运行这个脚本,所以我决定使用 inotify

我的脚本是这样的

#!/bin/bash

inotifywait -m -r -e create "<directory_of_files_to_upload>" | while read NEWFILE
do
        aws s3 sync sunshine s3://turnaround-sunshine/
done

我的问题是两个方面

1.当我运行这个脚本时,它会接管终端,所以我不能做任何其他事情

[ec2-user@ip-xxx-xx-xx-xx s3fs-fuse]$ ./Script.sh 
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
  1. 它在我从本地上传文件但不上传整个文件时运行。ec2 中的文件是 2.7MB,但在 s3 中只有 ~350KB。当我自己运行 aws 命令而不进行 inotify 时,它可以正常工作(整个文件被上传)。当我将文件上传到受监控的目录时,程序也会输出(如下)。

    上传:sunshine/turnaroundtest.json 到 s3://turnaround-sunshine/turnaroundtest.json

4

1 回答 1

1
  1. 您可以在后台运行脚本:

    ./Script.sh &
    

    或者您可以打开第二个终端窗口来运行它。

  2. 您的脚本在创建文件后立即开始上传文件,这不允许作者有时间完成编写它。没有可靠的方法来判断文件何时完成。解决此问题的最佳方法是更改​​书写应用程序。它应该首先将文件写入另一个目录,然后在完成后将其移动到该目录。只要两个目录在同一个文件系统中,移动就是原子的,所以上传脚本只会看到完成的文件。

    如果由于某种原因不能使用两个目录,则可以使用文件名模式。它可以将文件写入<filename>.temp,然后最后将其重命名为<filename>. 然后您的脚本可以忽略.temp文件:

    while read newfile; 
    do 
        case "$newfile" in
        *.temp) ;;
        *) aws s3 sync sunshine s3://turnaround-sunshine/ ;;
        esac
    done
    
于 2018-06-21T22:11:03.000 回答