Skip to content

Commit 8749553

Browse files
committed
upload scripts
1 parent 4382752 commit 8749553

2 files changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash#
2+
3+
# === Config ===
4+
5+
BUCKET_NAME="eligibility-signposting-api-dev-eli-rules"
6+
S3_PREFIX="manual-uploads"
7+
8+
# === Usage Info ===
9+
# make it executable using - chmod +x manual_config_upload.sh
10+
11+
if [ "$#" -lt 1 ]; then
12+
echo "Usage: $0 <file.json> [bucket_name]"
13+
echo
14+
echo "Before running this script, you should either:"
15+
echo " • Run 'aws configure'"
16+
echo " • OR export these variables:"
17+
echo " export AWS_ACCESS_KEY_ID=..."
18+
echo " export AWS_SECRET_ACCESS_KEY=..."
19+
echo " export AWS_SESSION_TOKEN=... # if using temporary credentials"
20+
echo
21+
exit 1
22+
fi
23+
FILE="$1"
24+
BUCKET="${2:-$BUCKET_NAME}"
25+
26+
# === Check dependencies ===
27+
28+
if ! command -v jq >/dev/null 2>&1; then
29+
echo " 'jq' is not installed. Please install it (e.g., sudo apt install jq)"
30+
exit 1
31+
fi
32+
33+
# === Validate JSON ===
34+
35+
if ! jq empty "$FILE" >/dev/null 2>&1; then
36+
echo " Invalid JSON in file: $FILE"
37+
exit 1
38+
fi
39+
40+
# === Prompt for AWS credentials if not set ===
41+
42+
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
43+
echo "AWS credentials not found in environment. Let's set them now:"
44+
read -p "Enter AWS_ACCESS_KEY_ID: " AWS_ACCESS_KEY_ID
45+
read -s -p "Enter AWS_SECRET_ACCESS_KEY: " AWS_SECRET_ACCESS_KEY
46+
echo
47+
read -s -p "Enter AWS_SESSION_TOKEN (leave blank if not needed): " AWS_SESSION_TOKEN
48+
echo export AWS_ACCESS_KEY_ID
49+
export AWS_SECRET_ACCESS_KEY
50+
export AWS_SESSION_TOKEN
51+
fi
52+
53+
# === Confirm credentials work ===
54+
55+
aws sts get-caller-identity >/dev/null 2>&1
56+
if [ $? -ne 0 ]; then
57+
echo "Failed to authenticate with AWS. Please check your credentials."
58+
exit 1
59+
60+
fi
61+
62+
# === Create unique key ===
63+
64+
BASENAME=$(basename "$FILE" .json)
65+
TIMESTAMP=$(date +"%Y%m%dT%H%M%S")
66+
S3_KEY="${S3_PREFIX}/${BASENAME}_${TIMESTAMP}.json"
67+
68+
# === Upload ===
69+
70+
echo " JSON is valid. Uploading to s3://$BUCKET/$S3_KEY ..."
71+
aws s3 cp "$FILE" "s3://$BUCKET/$S3_KEY" --content-type "application/json"
72+
73+
if [ $? -eq 0 ]; then
74+
echo " Upload complete."
75+
else
76+
echo "Upload failed."
77+
fi
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
3+
# === Config ===
4+
DEFAULT_TABLE="eligibility_data_store"
5+
REGION="eu-west-2"
6+
7+
# === Usage Check ===
8+
9+
if [ "$#" -lt 1 ]; then
10+
echo "Usage: $0 <item.json> [table_name]"
11+
echo
12+
echo "Before running this, make sure AWS credentials are set either via:"
13+
echo " • 'aws configure'"
14+
echo " • OR by exporting:"
15+
echo " export AWS_ACCESS_KEY_ID=..."
16+
echo " export AWS_SECRET_ACCESS_KEY=..."
17+
echo " export AWS_SESSION_TOKEN=..."
18+
exit 1
19+
fi
20+
21+
FILE="$1"
22+
TABLE="${2:-$DEFAULT_TABLE}"
23+
24+
# === Validate JSON ===
25+
26+
if ! jq empty "$FILE" >/dev/null 2>&1; then
27+
echo "Invalid JSON in file: $FILE"
28+
exit 1
29+
fi
30+
31+
# === Prompt for AWS credentials if not set ===
32+
33+
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
34+
echo "AWS credentials not found. Let's set them now:"
35+
read -p "Enter AWS_ACCESS_KEY_ID: " AWS_ACCESS_KEY_ID
36+
read -s -p "Enter AWS_SECRET_ACCESS_KEY: " AWS_SECRET_ACCESS_KEY
37+
echo
38+
read -s -p "Enter AWS_SESSION_TOKEN (leave blank if not needed): " AWS_SESSION_TOKEN
39+
echo
40+
41+
export AWS_ACCESS_KEY_ID
42+
export AWS_SECRET_ACCESS_KEY
43+
export AWS_SESSION_TOKEN
44+
fi
45+
46+
# === Check AWS auth ===
47+
48+
aws sts get-caller-identity >/dev/null 2>&1
49+
if [ $? -ne 0 ]; then
50+
echo "Failed to authenticate with AWS. Check credentials."
51+
exit 1
52+
fi
53+
54+
# === Upload to DynamoDB ===
55+
56+
echo "Uploading item from $FILE to table $TABLE ..."
57+
aws dynamodb put-item \
58+
--table-name "$TABLE" \
59+
--item "file://$FILE" \
60+
--region "$REGION"
61+
62+
if [ $? -eq 0 ]; then
63+
echo "Upload complete."
64+
else
65+
echo "Upload failed."
66+
fi

0 commit comments

Comments
 (0)