Comece a usar o Amazon S3 usando a CLI - AWS Exemplos de código do SDK

Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Comece a usar o Amazon S3 usando a CLI

O código de exemplo a seguir mostra como:

  • Crie um bucket S3 com nomenclatura exclusiva e configuração regional

  • Defina as configurações de segurança do bucket, incluindo bloqueio de acesso público

  • Ative o controle de versão e a criptografia padrão para proteção de dados

  • Faça upload de objetos com e sem metadados personalizados

  • Baixe objetos do bucket para o armazenamento local

  • Copie objetos dentro do bucket para organizar os dados em pastas

  • Listar conteúdos e objetos do bucket com prefixos específicos

  • Adicione tags aos buckets para gerenciamento de recursos

  • Limpe todos os recursos, incluindo objetos versionados

Bash
AWS CLI com script Bash
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no repositório Sample Developer Tutorials.

#!/bin/bash # 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 # 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" } # Function to handle errors handle_error() { log "ERROR: $1" exit 1 } # 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 fi } # Function to delete all versions of objects in a bucket delete_all_versions() { local bucket=$1 log "Deleting all object versions from bucket $bucket..." # 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 # 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 } # Function to handle cleanup on exit cleanup_handler() { local exit_code=$1 # Only run cleanup if it hasn't been run already if [ -z "$CLEANUP_DONE" ]; then cleanup fi exit $exit_code } # Function to clean up resources cleanup() { 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 fi # 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 fi # 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" fi fi # Clean up local files log "Removing local files..." rm -f sample-file.txt sample-document.txt downloaded-sample-file.txt empty-file.tmp log "Cleanup completed." } # Generate a random bucket name generate_bucket_name() { local hex_id hex_id=$(openssl rand -hex 6) echo "demo-s3-bucket-$hex_id" } # Main script execution main() { log "Starting Amazon S3 Getting Started Tutorial" # Generate a unique bucket name BUCKET_NAME=$(generate_bucket_name) log "Generated bucket name: $BUCKET_NAME" # Step 1: Create a bucket log "Step 1: Creating S3 bucket..." # Get the current region or default to us-east-1 REGION=$(aws configure get region) REGION=${REGION:-us-east-1} log "Using region: $REGION" if [ "$REGION" = "us-east-1" ]; then aws s3api create-bucket --bucket "$BUCKET_NAME" || handle_error "Failed to create bucket" else aws s3api create-bucket \ --bucket "$BUCKET_NAME" \ --region "$REGION" \ --create-bucket-configuration LocationConstraint="$REGION" || handle_error "Failed to create bucket" fi log "Bucket created successfully" # Configure bucket settings log "Configuring bucket settings..." # Block public access (security best practice) log "Blocking public access..." aws s3api put-public-access-block \ --bucket "$BUCKET_NAME" \ --public-access-block-configuration "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true" || handle_error "Failed to configure public access block" # Enable versioning log "Enabling versioning..." aws s3api put-bucket-versioning \ --bucket "$BUCKET_NAME" \ --versioning-configuration Status=Enabled || handle_error "Failed to enable versioning" # Set default encryption log "Setting default encryption..." aws s3api put-bucket-encryption \ --bucket "$BUCKET_NAME" \ --server-side-encryption-configuration '{"Rules": [{"ApplyServerSideEncryptionByDefault": {"SSEAlgorithm": "AES256"}}]}' || handle_error "Failed to set encryption" # Step 2: Upload an object log "Step 2: Uploading objects to bucket..." # Create a sample file echo "This is a sample file for the S3 tutorial." > sample-file.txt # Upload the file aws s3api put-object \ --bucket "$BUCKET_NAME" \ --key "sample-file.txt" \ --body "sample-file.txt" || handle_error "Failed to upload object" log "Object uploaded successfully" # Upload with metadata echo "This is a document with metadata." > sample-document.txt aws s3api put-object \ --bucket "$BUCKET_NAME" \ --key "documents/sample-document.txt" \ --body "sample-document.txt" \ --content-type "text/plain" \ --metadata "author=AWSDocumentation,purpose=tutorial" || handle_error "Failed to upload object with metadata" log "Object with metadata uploaded successfully" # Step 3: Download an object log "Step 3: Downloading object from bucket..." aws s3api get-object \ --bucket "$BUCKET_NAME" \ --key "sample-file.txt" \ "downloaded-sample-file.txt" || handle_error "Failed to download object" log "Object downloaded successfully" # Check if an object exists log "Checking if object exists..." aws s3api head-object \ --bucket "$BUCKET_NAME" \ --key "sample-file.txt" || handle_error "Object does not exist" log "Object exists" # Step 4: Copy object to a folder log "Step 4: Copying object to a folder..." # Create a folder structure using a temporary empty file log "Creating folder structure..." touch empty-file.tmp aws s3api put-object \ --bucket "$BUCKET_NAME" \ --key "favorite-files/" \ --body empty-file.tmp || handle_error "Failed to create folder" # Copy the object log "Copying object..." aws s3api copy-object \ --bucket "$BUCKET_NAME" \ --copy-source "$BUCKET_NAME/sample-file.txt" \ --key "favorite-files/sample-file.txt" || handle_error "Failed to copy object" log "Object copied successfully" # List objects in the bucket log "Listing all objects in the bucket..." aws s3api list-objects-v2 \ --bucket "$BUCKET_NAME" \ --query 'Contents[].Key' \ --output table || handle_error "Failed to list objects" # List objects with a specific prefix log "Listing objects in the favorite-files folder..." aws s3api list-objects-v2 \ --bucket "$BUCKET_NAME" \ --prefix "favorite-files/" \ --query 'Contents[].Key' \ --output table || handle_error "Failed to list objects with prefix" # Add tags to the bucket log "Adding tags to the bucket..." aws s3api put-bucket-tagging \ --bucket "$BUCKET_NAME" \ --tagging 'TagSet=[{Key=Project,Value=S3Tutorial},{Key=Environment,Value=Demo}]' || handle_error "Failed to add tags" log "Tags added successfully" log "Tutorial completed successfully!" } # Execute the main function main