Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

powershell scripting for s3 #1

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
17 changes: 0 additions & 17 deletions s3/bash-scripts/create-bucket

This file was deleted.

23 changes: 23 additions & 0 deletions s3/bash-scripts/create-bucket.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

# Prompt user to enter bucket name
read -p "Enter the bucket name: " BUCKET_NAME

# Prompt user to enter region
read -p "Enter the region: " REGION

# Check if either bucket name or region is empty
if [ -z "$BUCKET_NAME" ] || [ -z "$REGION" ]; then
echo "Both bucket name and region must be provided."
exit 1
fi

# Create the bucket with the specified name and region
aws s3api create-bucket --bucket "$BUCKET_NAME" --region "$REGION"

# Check if bucket creation was successful
if [ $? -eq 0 ]; then
echo "Bucket '$BUCKET_NAME' created successfully in region '$REGION'."
else
echo "Failed to create bucket '$BUCKET_NAME' in region '$REGION'."
fi
14 changes: 0 additions & 14 deletions s3/bash-scripts/delete-bucket

This file was deleted.

36 changes: 36 additions & 0 deletions s3/bash-scripts/delete-bucket.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

echo "List of existing buckets:"
# List existing buckets
buckets=$(aws s3api list-buckets --query 'Buckets[].Name' --output text)

# Check if there are any buckets
if [ -z "$buckets" ]; then
echo "No buckets found."
exit 0
fi

# Print each bucket name with index
echo "Available buckets:"
select bucket in $buckets; do
break
done

# Prompt user for confirmation before deleting the bucket
read -p "Are you sure you want to delete bucket '$bucket'? (yes/no): " confirmation

# Check user confirmation
if [ "$confirmation" != "yes" ]; then
echo "Bucket deletion canceled."
exit 0
fi

# Delete the specified bucket
aws s3api delete-bucket --bucket "$bucket"

# Check if bucket deletion was successful
if [ $? -eq 0 ]; then
echo "Bucket '$bucket' deleted successfully."
else
echo "Failed to delete bucket '$bucket'."
fi
20 changes: 0 additions & 20 deletions s3/bash-scripts/delete-objects

This file was deleted.

83 changes: 83 additions & 0 deletions s3/bash-scripts/delete-objects.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash

echo "== Delete S3 Objects =="

# List all S3 buckets
buckets=$(aws s3api list-buckets --query "Buckets[].Name" --output text)

# Check if there are any buckets
if [ -z "$buckets" ]; then
echo "No buckets found."
exit 0
fi

# Print each bucket name with index
echo "Available buckets:"
select bucket in $buckets; do
break
done

# List objects in the selected bucket
objects=$(aws s3 ls "s3://$bucket" --recursive --human-readable --summarize)

# Check if there are any objects
if [ -z "$objects" ]; then
echo "No objects found in bucket '$bucket'."
exit 0
fi

# Print objects in the selected bucket with index
echo "Objects in bucket '$bucket':"
echo "$objects" | awk '{print NR, $0}'

# Prompt user to choose to delete all objects or specific ones
read -p "Do you want to delete all objects? (yes/no): " DELETE_ALL

# Check user's choice
if [ "$DELETE_ALL" = "yes" ]; then
# Confirm with user before deleting all objects
read -p "Are you sure you want to delete all objects from bucket '$bucket'? (yes/no): " CONFIRM
if [ "$CONFIRM" != "yes" ]; then
echo "Deletion canceled."
exit 0
fi

# Delete all objects from the bucket
aws s3 rm "s3://$bucket" --recursive

# Check if all objects deletion was successful
if [ $? -eq 0 ]; then
echo "All objects deleted successfully from bucket '$bucket'."
else
echo "Failed to delete all objects from bucket '$bucket'."
fi
else
# Prompt user to enter the index of the object to delete
read -p "Enter the index of the object to delete (or 0 to cancel): " object_index

# Validate user input
if [ "$object_index" -eq 0 ]; then
echo "Deletion canceled."
exit 0
fi

# Extract the object key from the selected index
object_key=$(echo "$objects" | sed -n "${object_index}p" | awk '{print $NF}')

# Confirm with user before deleting the object
read -p "Are you sure you want to delete object '$object_key' from bucket '$bucket'? (yes/no): " CONFIRM
if [ "$CONFIRM" != "yes" ]; then
echo "Deletion canceled."
exit 0
fi

# Delete the selected object from the bucket
aws s3 rm "s3://$bucket/$object_key"

# Check if object deletion was successful
if [ $? -eq 0 ]; then
echo "Object '$object_key' deleted successfully from bucket '$bucket'."
else
echo "Failed to delete object '$object_key' from bucket '$bucket'."
fi
fi
2 changes: 0 additions & 2 deletions s3/bash-scripts/get-newest-bucket

This file was deleted.

5 changes: 0 additions & 5 deletions s3/bash-scripts/list-buckets

This file was deleted.

25 changes: 25 additions & 0 deletions s3/bash-scripts/list-buckets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

echo "== Listing S3 Buckets =="

# List all S3 buckets along with their creation dates
buckets_with_dates=$(aws s3 ls | awk '{print $3, $1, $2}')

# Check if there are any buckets
if [ -z "$buckets_with_dates" ]; then
echo "No buckets found."
exit 0
fi

# Sort buckets by creation date (oldest first)
sorted_buckets=$(echo "$buckets_with_dates" | sort -k 2)

# Reverse the sorted list to have the newest bucket listed first
newest_first=$(echo "$sorted_buckets" | tac)

# Extract bucket names from the sorted list
bucket_names=$(echo "$newest_first" | awk '{print $1}')

# Print each bucket name
echo "Found bucket or buckets (newest first):"
echo "$bucket_names"
14 changes: 0 additions & 14 deletions s3/bash-scripts/list-objects

This file was deleted.

13 changes: 13 additions & 0 deletions s3/bash-scripts/list-objects.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Prompt user to enter the bucket name
read -p "Enter the bucket name: " BUCKET_NAME

# Check if bucket name is empty
if [ -z "$BUCKET_NAME" ]; then
echo "Bucket name must be provided."
exit 1
fi

# List objects in the specified bucket
aws s3api list-objects --bucket "$BUCKET_NAME"
22 changes: 0 additions & 22 deletions s3/bash-scripts/put-object

This file was deleted.

34 changes: 34 additions & 0 deletions s3/bash-scripts/put-object.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

# Set the script to exit immediately if any command fails
set -e

# Heading
echo "Creating 5 Random Files and Displaying Directory Tree"

# Define the output directory
output_dir="/tmp/s3-temp-bash-scripts"

# Remove the directory if it already exists
if [ -d "$output_dir" ]; then
echo "Removing existing directory: $output_dir"
rm -rf "$output_dir"
fi

# Create the directory
mkdir -p "$output_dir"

# Generate 5 random files
for i in {1..5}; do
# Generate a random filename
filename="$output_dir/file$i.txt"
# Generate random content
content=$(openssl rand -base64 32)
# Write content to file
echo "$content" > "$filename"
echo "Created file: $filename"
done

# Display directory tree
echo "Directory Tree:"
tree "$output_dir"
45 changes: 0 additions & 45 deletions s3/bash-scripts/sync

This file was deleted.

Loading