Skip to content

Commit f5f61f7

Browse files
committed
use if-none-match
Signed-off-by: Andrew Duffy <andrew@a10y.dev>
1 parent 2e050a3 commit f5f61f7

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

scripts/cat-s3.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@ n_failures=0
1313

1414
while (( n_failures < 100 )); do
1515
current_etag=$(aws s3api head-object --bucket "$bucket" --key "$key" --query ETag --output text 2>/dev/null) || {
16-
# File doesn't exist yet, create it fresh
16+
# File doesn't exist yet, try to create it fresh
1717
echo "File does not exist in S3, creating new file."
1818
if [[ "$key" =~ \.gz$ ]]; then
1919
gzip -c "$local_file_to_concatenate" > "$local_concatenated"
2020
else
2121
cp "$local_file_to_concatenate" "$local_concatenated"
2222
fi
23-
aws s3api put-object --bucket "$bucket" --key "$key" --body "$local_concatenated"
24-
echo "File created and uploaded successfully."
25-
exit 0
23+
# Use --if-none-match to avoid race with concurrent writers
24+
aws s3api put-object --bucket "$bucket" --key "$key" --body "$local_concatenated" --if-none-match "*" && {
25+
echo "File created and uploaded successfully."
26+
exit 0
27+
}
28+
# Another writer created the file first, retry to concatenate with it
29+
echo "File was created by another writer, retrying to concatenate."
30+
n_failures=$(( n_failures + 1 ))
31+
sleep 0.1
32+
continue
2633
}
2734
if [[ "$current_etag" == "null" ]]; then
2835
echo "Failed to retrieve ETag. Exiting."

0 commit comments

Comments
 (0)