Skip to content

Commit 2fb670f

Browse files
committed
Add storage tutorials (batch 9)
1 parent 49f07d9 commit 2fb670f

5 files changed

Lines changed: 50 additions & 0 deletions

File tree

tuts/198-s3-cors/s3-cors.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/tut.log") 2>&1
3+
REGION=${AWS_DEFAULT_REGION:-$(aws configure get region 2>/dev/null)}; [ -z "$REGION" ] && echo "ERROR: No region" && exit 1; export AWS_DEFAULT_REGION="$REGION"; echo "Region: $REGION"
4+
RANDOM_ID=$(openssl rand -hex 4); ACCOUNT=$(aws sts get-caller-identity --query Account --output text); B="cors-tut-${RANDOM_ID}-${ACCOUNT}"
5+
cleanup() { aws s3 rb "s3://$B" 2>/dev/null; rm -rf "$WORK_DIR"; echo "Done."; }
6+
echo "Step 1: Creating bucket"; aws s3api create-bucket --bucket "$B" > /dev/null
7+
echo "Step 2: Setting CORS"; aws s3api put-bucket-cors --bucket "$B" --cors-configuration '{"CORSRules":[{"AllowedOrigins":["https://example.com"],"AllowedMethods":["GET","PUT"],"AllowedHeaders":["*"],"MaxAgeSeconds":3600}]}'
8+
echo "Step 3: Getting CORS"; aws s3api get-bucket-cors --bucket "$B" --query 'CORSRules[0].{Origins:AllowedOrigins,Methods:AllowedMethods}' --output table
9+
echo "Do you want to clean up? (y/n): "; read -r C; [[ "$C" =~ ^[Yy]$ ]] && cleanup
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/tut.log") 2>&1
3+
REGION=${AWS_DEFAULT_REGION:-$(aws configure get region 2>/dev/null)}; [ -z "$REGION" ] && echo "ERROR: No region" && exit 1; export AWS_DEFAULT_REGION="$REGION"; echo "Region: $REGION"
4+
echo "Step 1: Listing objects with storage classes"
5+
BUCKET=$(aws s3api list-buckets --query 'Buckets[0].Name' --output text)
6+
[ -n "$BUCKET" ] && [ "$BUCKET" != "None" ] && aws s3api list-objects-v2 --bucket "$BUCKET" --max-keys 10 --query 'Contents[].{Key:Key,Size:Size,Class:StorageClass}' --output table || echo " No buckets"
7+
echo "Step 2: Storage class reference"
8+
echo " STANDARD: Default, frequently accessed"
9+
echo " STANDARD_IA: Infrequent access, lower cost"
10+
echo " ONEZONE_IA: Single AZ, lowest IA cost"
11+
echo " INTELLIGENT_TIERING: Auto-tiering"
12+
echo " GLACIER_IR: Instant retrieval archive"
13+
echo " GLACIER: Flexible retrieval (minutes to hours)"
14+
echo " DEEP_ARCHIVE: Lowest cost (12-48 hours)"
15+
echo ""; echo "Tutorial complete. Read-only."; rm -rf "$WORK_DIR"

tuts/206-s3-metrics/s3-metrics.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/tut.log") 2>&1
3+
REGION=${AWS_DEFAULT_REGION:-$(aws configure get region 2>/dev/null)}; [ -z "$REGION" ] && echo "ERROR: No region" && exit 1; export AWS_DEFAULT_REGION="$REGION"; echo "Region: $REGION"
4+
echo "Step 1: Listing buckets with sizes"
5+
aws s3api list-buckets --query 'Buckets[:10].{Name:Name,Created:CreationDate}' --output table
6+
echo "Step 2: Getting bucket metrics (request count)"
7+
B=$(aws s3api list-buckets --query 'Buckets[0].Name' --output text)
8+
[ -n "$B" ] && [ "$B" != "None" ] && aws cloudwatch get-metric-statistics --namespace AWS/S3 --metric-name NumberOfObjects --dimensions Name=BucketName,Value="$B" Name=StorageType,Value=AllStorageTypes --start-time "$(date -u -d '2 days ago' +%Y-%m-%dT%H:%M:%SZ)" --end-time "$(date -u +%Y-%m-%dT%H:%M:%SZ)" --period 86400 --statistics Average --query 'Datapoints[0].Average' --output text 2>/dev/null || echo " No metrics"
9+
echo ""; echo "Tutorial complete. Read-only."; rm -rf "$WORK_DIR"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/tut.log") 2>&1
3+
REGION=${AWS_DEFAULT_REGION:-$(aws configure get region 2>/dev/null)}; [ -z "$REGION" ] && echo "ERROR: No region" && exit 1; export AWS_DEFAULT_REGION="$REGION"; echo "Region: $REGION"
4+
echo "Step 1: Listing all buckets"; aws s3api list-buckets --query 'Buckets[].{Name:Name,Created:CreationDate}' --output table
5+
echo "Step 2: Bucket count"; echo " Total: $(aws s3api list-buckets --query 'Buckets | length(@)' --output text) buckets"
6+
echo "Step 3: Checking public access block"; B=$(aws s3api list-buckets --query 'Buckets[0].Name' --output text); [ -n "$B" ] && [ "$B" != "None" ] && aws s3api get-public-access-block --bucket "$B" --query 'PublicAccessBlockConfiguration' --output table 2>/dev/null || echo " No public access block"
7+
echo ""; echo "Tutorial complete. Read-only."; rm -rf "$WORK_DIR"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/tut.log") 2>&1
3+
REGION=${AWS_DEFAULT_REGION:-$(aws configure get region 2>/dev/null)}; [ -z "$REGION" ] && echo "ERROR: No region" && exit 1; export AWS_DEFAULT_REGION="$REGION"; echo "Region: $REGION"
4+
RANDOM_ID=$(openssl rand -hex 4); ACCOUNT=$(aws sts get-caller-identity --query Account --output text); B="tag-tut-${RANDOM_ID}-${ACCOUNT}"
5+
cleanup() { aws s3 rb "s3://$B" 2>/dev/null; rm -rf "$WORK_DIR"; echo "Done."; }
6+
echo "Step 1: Creating bucket"; aws s3api create-bucket --bucket "$B" > /dev/null
7+
echo "Step 2: Adding tags"; aws s3api put-bucket-tagging --bucket "$B" --tagging 'TagSet=[{Key=Environment,Value=tutorial},{Key=Project,Value=tagging-demo},{Key=Owner,Value=tutorial-user}]'
8+
echo "Step 3: Getting tags"; aws s3api get-bucket-tagging --bucket "$B" --query 'TagSet[].{Key:Key,Value:Value}' --output table
9+
echo "Step 4: Deleting tags"; aws s3api delete-bucket-tagging --bucket "$B"; echo " Tags deleted"
10+
echo "Do you want to clean up? (y/n): "; read -r C; [[ "$C" =~ ^[Yy]$ ]] && cleanup

0 commit comments

Comments
 (0)