Created
March 21, 2024 12:23
-
-
Save zirkelc/2136236fe8a25856594f14d2701df98b to your computer and use it in GitHub Desktop.
Raycast Script for S3 Download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Required parameters: | |
# @raycast.schemaVersion 1 | |
# @raycast.title S3 Download | |
# @raycast.mode fullOutput | |
# Optional parameters: | |
# @raycast.icon 🤖 | |
# @raycast.packageName AWS | |
# Documentation: | |
# @raycast.description Download S3 Object via URL | |
# @raycast.argument1 { "type": "text", "placeholder": "s3://bucket/key" } | |
S3_URL=$1 | |
# Check if AWS CLI is installed | |
if ! command -v aws &> /dev/null; then | |
echo "AWS CLI could not be found" | |
echo "Install AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html" | |
exit 1 | |
fi | |
# Check URL starts with s3:// | |
if [[ $S3_URL != s3://* ]]; then | |
echo "Invalid S3 URL: $S3_URL" | |
echo "URL must start with s3://" | |
exit 1 | |
fi | |
# Remove the 's3://' prefix | |
URL_WITHOUT_SCHEME=${S3_URL#s3://} | |
# If URL contains S3 host name s3.amazonaws.com or s3.<region>.amazonaws.com, extract bucket and key | |
if [[ $URL_WITHOUT_SCHEME == *amazonaws.com* ]]; then | |
# Extract the bucket and key after the amazonaws.com/ | |
BUCKET_AND_KEY=${URL_WITHOUT_SCHEME#*amazonaws.com/} | |
BUCKET=$(echo $BUCKET_AND_KEY | cut -d'/' -f1) | |
KEY=$(echo $BUCKET_AND_KEY | cut -d'/' -f2-) | |
else | |
# Direct bucket and key extraction | |
BUCKET=$(echo $URL_WITHOUT_SCHEME | cut -d'/' -f1) | |
KEY=$(echo $URL_WITHOUT_SCHEME | cut -d'/' -f2-) | |
fi | |
# Check if bucket and key are empty | |
if [[ -z "$BUCKET" ]] || [[ -z "$KEY" ]]; then | |
echo "Invalid S3 URL: $S3_URL" | |
echo "URL must be in the format s3://<bucket>/<key> or s3://s3.amazonaws.com/<bucket>/<key> or s3://s3.<region>.amazonaws.com/<bucket>/<key>" | |
exit 1 | |
fi | |
echo "Bucket: $BUCKET" | |
echo "Key: $KEY" | |
# Download file from S3 to local Downloads folder | |
echo "Downloading $S3_URL to $HOME/Downloads/..." | |
aws s3 cp "s3://$BUCKET/$KEY" "$HOME/Downloads/" | |
# Open the Downloads folder | |
open "$HOME/Downloads/" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment