89 lines
2.0 KiB
Bash
Executable File
89 lines
2.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# post3 usage with the AWS CLI
|
|
#
|
|
# Prerequisites:
|
|
# 1. post3-server running: mise run up && mise run dev
|
|
# 2. AWS CLI installed: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html
|
|
#
|
|
# Usage:
|
|
# bash examples/aws-cli.sh
|
|
#
|
|
# Or:
|
|
# mise run example:cli
|
|
|
|
set -euo pipefail
|
|
|
|
ENDPOINT="http://localhost:9000"
|
|
BUCKET="cli-demo"
|
|
|
|
# AWS CLI needs credentials even though post3 doesn't validate them (yet)
|
|
export AWS_ACCESS_KEY_ID=test
|
|
export AWS_SECRET_ACCESS_KEY=test
|
|
export AWS_DEFAULT_REGION=us-east-1
|
|
|
|
aws() {
|
|
command aws --endpoint-url "$ENDPOINT" "$@"
|
|
}
|
|
|
|
echo "=== post3 AWS CLI Demo ==="
|
|
|
|
# Create a bucket
|
|
echo ""
|
|
echo "--- Creating bucket '$BUCKET'"
|
|
aws s3api create-bucket --bucket "$BUCKET"
|
|
|
|
# List buckets
|
|
echo ""
|
|
echo "--- Listing buckets"
|
|
aws s3api list-buckets
|
|
|
|
# Upload a file
|
|
echo ""
|
|
echo "--- Uploading hello.txt"
|
|
echo "Hello from the AWS CLI!" | aws s3 cp - "s3://$BUCKET/hello.txt"
|
|
|
|
# Upload with metadata
|
|
echo ""
|
|
echo "--- Uploading report.txt with metadata"
|
|
echo "Report content" | aws s3 cp - "s3://$BUCKET/report.txt" \
|
|
--metadata "author=alice,version=1"
|
|
|
|
# List objects
|
|
echo ""
|
|
echo "--- Listing objects"
|
|
aws s3api list-objects-v2 --bucket "$BUCKET"
|
|
|
|
# List with prefix
|
|
echo ""
|
|
echo "--- Uploading docs/readme.md and docs/guide.md"
|
|
echo "# README" | aws s3 cp - "s3://$BUCKET/docs/readme.md"
|
|
echo "# Guide" | aws s3 cp - "s3://$BUCKET/docs/guide.md"
|
|
|
|
echo ""
|
|
echo "--- Listing objects with prefix 'docs/'"
|
|
aws s3api list-objects-v2 --bucket "$BUCKET" --prefix "docs/"
|
|
|
|
# Download a file
|
|
echo ""
|
|
echo "--- Downloading hello.txt"
|
|
aws s3 cp "s3://$BUCKET/hello.txt" -
|
|
|
|
# Head object (metadata)
|
|
echo ""
|
|
echo "--- Head object report.txt"
|
|
aws s3api head-object --bucket "$BUCKET" --key "report.txt"
|
|
|
|
# Delete objects
|
|
echo ""
|
|
echo "--- Cleaning up"
|
|
aws s3 rm "s3://$BUCKET/hello.txt"
|
|
aws s3 rm "s3://$BUCKET/report.txt"
|
|
aws s3 rm "s3://$BUCKET/docs/readme.md"
|
|
aws s3 rm "s3://$BUCKET/docs/guide.md"
|
|
|
|
# Delete bucket
|
|
aws s3api delete-bucket --bucket "$BUCKET"
|
|
|
|
echo ""
|
|
echo "=== Done ==="
|