AWS code-library documentation change
Summary
Complete rewrite of S3 Getting Started tutorial with restructured code example, improved error handling, better resource tracking, and more detailed step-by-step instructions
Security assessment
The changes add explicit security documentation including SSE-S3 encryption with BucketKeyEnabled, public access blocking configuration, and improved cleanup procedures. However, there's no evidence this addresses a specific security vulnerability - it's a general tutorial improvement that includes security best practices.
Diff
diff --git a/code-library/latest/ug/s3_example_s3_GettingStarted_section.md b/code-library/latest/ug/s3_example_s3_GettingStarted_section.md index 1d5ec2d7a..59fcbbbd2 100644 --- a//code-library/latest/ug/s3_example_s3_GettingStarted_section.md +++ b//code-library/latest/ug/s3_example_s3_GettingStarted_section.md @@ -9 +9 @@ There are more AWS SDK examples available in the [AWS Doc SDK Examples](https:// -# Get started with Amazon S3 using the CLI +# Getting started with Amazon S3 @@ -13 +13 @@ The following code example shows how to: - * Create an S3 bucket with unique naming and regional configuration + * Create your first S3 bucket @@ -15 +15 @@ The following code example shows how to: - * Configure bucket security settings including public access blocking + * Upload an object @@ -17 +17 @@ The following code example shows how to: - * Enable versioning and default encryption for data protection + * Enable versioning @@ -19 +19 @@ The following code example shows how to: - * Upload objects with and without custom metadata + * Configure default encryption @@ -21 +21 @@ The following code example shows how to: - * Download objects from the bucket to local storage + * Add tags to your bucket @@ -23 +23 @@ The following code example shows how to: - * Copy objects within the bucket to organize data in folders + * List objects and versions @@ -25,5 +25 @@ The following code example shows how to: - * List bucket contents and objects with specific prefixes - - * Add tags to buckets for resource management - - * Clean up all resources including versioned objects + * Clean up resources @@ -45,0 +42,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru + # S3 Getting Started - Create a bucket, upload and download objects, copy to a + # folder prefix, enable versioning, configure encryption and public access + # blocking, tag the bucket, list objects and versions, and clean up. @@ -47,15 +46 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - # Amazon S3 Getting Started Tutorial Script - # This script demonstrates basic S3 operations including: - # - Creating a bucket - # - Configuring bucket settings - # - Uploading, downloading, and copying objects - # - Deleting objects and buckets - - # Latest fixes: - # 1. Fixed folder creation using temporary file - # 2. Corrected versioned object deletion in cleanup - # 3. Improved error handling for cleanup operations - - # Set up error handling - set -e - trap 'cleanup_handler $?' EXIT + set -eE @@ -63,8 +48,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - # Log file setup - LOG_FILE="s3-tutorial-$(date +%Y%m%d-%H%M%S).log" - exec > >(tee -a "$LOG_FILE") 2>&1 - - # Function to log messages - log() { - echo "[$(date +"%Y-%m-%d %H:%M:%S")] $1" - } + # ============================================================================ + # Prerequisites check + # ============================================================================ @@ -72,3 +52,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - # Function to handle errors - handle_error() { - log "ERROR: $1" + CONFIGURED_REGION=$(aws configure get region 2>/dev/null || true) + if [ -z "$CONFIGURED_REGION" ] && [ -z "$AWS_DEFAULT_REGION" ] && [ -z "$AWS_REGION" ]; then + echo "ERROR: No AWS region configured. Run 'aws configure' or set AWS_DEFAULT_REGION." @@ -76,8 +55,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - } - - # Function to check if a bucket exists - bucket_exists() { - if aws s3api head-bucket --bucket "$1" 2>/dev/null; then - return 0 - else - return 1 @@ -85 +56,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - } @@ -87,4 +58,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - # Function to delete all versions of objects in a bucket - delete_all_versions() { - local bucket=$1 - log "Deleting all object versions from bucket $bucket..." + # ============================================================================ + # Setup: logging, temp directory, resource tracking + # ============================================================================ @@ -92,5 +62,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - # Get and delete all versions - versions=$(aws s3api list-object-versions --bucket "$bucket" --query 'Versions[].{Key:Key,VersionId:VersionId}' --output json 2>/dev/null) - if [ -n "$versions" ] && [ "$versions" != "null" ]; then - echo "{\"Objects\": $versions}" | aws s3api delete-objects --bucket "$bucket" --delete file:///dev/stdin >/dev/null 2>&1 || log "Warning: Some versions could not be deleted" - fi + UNIQUE_ID=$(cat /dev/urandom | tr -dc 'a-f0-9' | fold -w 12 | head -n 1) + BUCKET_NAME="s3api-${UNIQUE_ID}" @@ -98,6 +65,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - # Get and delete all delete markers - markers=$(aws s3api list-object-versions --bucket "$bucket" --query 'DeleteMarkers[].{Key:Key,VersionId:VersionId}' --output json 2>/dev/null) - if [ -n "$markers" ] && [ "$markers" != "null" ]; then - echo "{\"Objects\": $markers}" | aws s3api delete-objects --bucket "$bucket" --delete file:///dev/stdin >/dev/null 2>&1 || log "Warning: Some delete markers could not be deleted" - fi - } + TEMP_DIR=$(mktemp -d) + LOG_FILE="${TEMP_DIR}/s3-gettingstarted.log" + CREATED_RESOURCES=() @@ -105,3 +69 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - # Function to handle cleanup on exit - cleanup_handler() { - local exit_code=$1 + exec > >(tee -a "$LOG_FILE") 2>&1 @@ -109,4 +71,7 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - # Only run cleanup if it hasn't been run already - if [ -z "$CLEANUP_DONE" ]; then - cleanup - fi + echo "============================================" + echo "S3 Getting Started" + echo "============================================" + echo "Bucket name: ${BUCKET_NAME}" + echo "Temp directory: ${TEMP_DIR}" + echo "Log file: ${LOG_FILE}" + echo "" @@ -114,2 +79,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - exit $exit_code - } + # ============================================================================ + # Error handling and cleanup functions + # ============================================================================ @@ -117 +82,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - # Function to clean up resources @@ -119,17 +84,20 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - log "Starting cleanup process..." - CLEANUP_DONE=1 - - # List all resources created for confirmation - log "Resources created:" - if [ -n "$BUCKET_NAME" ]; then - log "- S3 Bucket: $BUCKET_NAME" - - # Only try to list objects if the bucket exists - if bucket_exists "$BUCKET_NAME"; then - # Check if any objects were created - OBJECTS=$(aws s3api list-objects-v2 --bucket "$BUCKET_NAME" --query 'Contents[].Key' --output text 2>/dev/null || echo "") - if [ -n "$OBJECTS" ]; then - log "- Objects in bucket:" - echo "$OBJECTS" | tr '\t' '\n' | while read -r obj; do - log " - $obj" - done + echo "" + echo "============================================" + echo "CLEANUP" + echo "============================================" + + # Delete all object versions and delete markers + echo "Listing all object versions in bucket..." + VERSIONS_OUTPUT=$(aws s3api list-object-versions \ + --bucket "$BUCKET_NAME" \ + --query "Versions[].{Key:Key,VersionId:VersionId}" \ + --output text 2>&1) || true + + if [ -n "$VERSIONS_OUTPUT" ] && [ "$VERSIONS_OUTPUT" != "None" ]; then + while IFS=$'\t' read -r KEY VERSION_ID; do + if [ -n "$KEY" ] && [ "$KEY" != "None" ]; then + echo "Deleting version: ${KEY} (${VERSION_ID})" + aws s3api delete-object \ + --bucket "$BUCKET_NAME" \ + --key "$KEY" \ + --version-id "$VERSION_ID" 2>&1 || echo "WARNING: Failed to delete version ${KEY} (${VERSION_ID})" @@ -137,6 +105 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - - # Ask for confirmation before cleanup - read -p "Do you want to proceed with cleanup and delete all resources? (y/n): " confirm - if [[ $confirm != [yY] && $confirm != [yY][eE][sS] ]]; then - log "Cleanup aborted by user." - return + done <<< "$VERSIONS_OUTPUT" @@ -145,8 +108,13 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - # Delete all versions of objects - delete_all_versions "$BUCKET_NAME" - - # Delete the bucket - log "Deleting bucket $BUCKET_NAME..." - aws s3api delete-bucket --bucket "$BUCKET_NAME" || log "Warning: Failed to delete bucket" - else - log "Bucket $BUCKET_NAME does not exist, skipping cleanup" + DELETE_MARKERS_OUTPUT=$(aws s3api list-object-versions \ + --bucket "$BUCKET_NAME" \ + --query "DeleteMarkers[].{Key:Key,VersionId:VersionId}" \ + --output text 2>&1) || true + + if [ -n "$DELETE_MARKERS_OUTPUT" ] && [ "$DELETE_MARKERS_OUTPUT" != "None" ]; then + while IFS=$'\t' read -r KEY VERSION_ID; do + if [ -n "$KEY" ] && [ "$KEY" != "None" ]; then + echo "Deleting delete marker: ${KEY} (${VERSION_ID})" + aws s3api delete-object \ + --bucket "$BUCKET_NAME" \