Created
October 7, 2024 01:21
-
-
Save uchks/93caf5b1e7f15c3d1d8998dbd6e5d5e6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
ENDPOINT_URL="https://<ACCOUNT_ID>.r2.cloudflarestorage.com" | |
CUSTOM_CDN_URL="https://<REPLACE_HERE>" | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
BLUE='\033[0;34m' | |
NC='\033[0m' # No Color | |
show_usage() { | |
echo -e "${BLUE}R2.sh${NC}" | |
echo | |
echo "Usage:" | |
echo " $0 <file_path> <bucket_name> <object_name>" | |
echo | |
echo "Arguments:" | |
echo " file_path : Path to the file you want to upload" | |
echo " bucket_name : Name of your R2 bucket" | |
echo " object_name : Desired name for the object in the bucket" | |
echo | |
echo "Example:" | |
echo " $0 ./myfile.pdf my-bucket folder/myfile.pdf" | |
} | |
# Check if AWS CLI is installed | |
check_aws_cli() { | |
if ! command -v aws &> /dev/null; then | |
echo -e "${RED}Error: AWS CLI is not installed${NC}" | |
echo "Please install AWS CLI first:" | |
echo " curl 'https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip' -o 'awscliv2.zip'" | |
echo " unzip awscliv2.zip" | |
echo " sudo ./aws/install" | |
exit 1 | |
fi | |
} | |
validate_inputs() { | |
if [ "$#" -lt 3 ]; then | |
show_usage | |
exit 1 | |
fi | |
local file_path=$1 | |
local bucket_name=$2 | |
local object_name=$3 | |
if [ ! -f "$file_path" ]; then | |
echo -e "${RED}Error: File '$file_path' does not exist${NC}" | |
exit 1 | |
fi | |
if [ ! -r "$file_path" ]; then | |
echo -e "${RED}Error: File '$file_path' is not readable${NC}" | |
exit 1 | |
fi | |
if [[ ! $bucket_name =~ ^[a-z0-9][a-z0-9-]*[a-z0-9]$ ]]; then | |
echo -e "${RED}Error: Invalid bucket name '$bucket_name'${NC}" | |
echo "Bucket names must:" | |
echo "- Contain only lowercase letters, numbers, and hyphens" | |
echo "- Start and end with a letter or number" | |
exit 1 | |
fi | |
} | |
calculate_sha256() { | |
local file_path=$1 | |
sha256sum "$file_path" | awk '{ print $1 }' | |
} | |
main() { | |
check_aws_cli | |
validate_inputs "$@" | |
local file_path=$1 | |
local bucket_name=$2 | |
local object_name=$3 | |
shift 3 | |
local file_size | |
file_size=$(wc -c < "$file_path") | |
local file_size_mb | |
file_size_mb=$(bc <<< "scale=2; $file_size/1048576") | |
local file_hash | |
file_hash=$(calculate_sha256 "$file_path") | |
echo -e "${BLUE}Starting upload:${NC}" | |
echo "- File: $file_path" | |
echo "- Size: ${file_size_mb}MB" | |
echo "- Bucket: $bucket_name" | |
echo "- Object: $object_name" | |
echo "- SHA-256 Hash: $file_hash" | |
echo | |
if aws --endpoint-url "$ENDPOINT_URL" s3 cp "$file_path" "s3://$bucket_name/$object_name"; then | |
echo -e "\n${GREEN}✓ Upload successful:${NC}" | |
local encoded_object_name | |
encoded_object_name=$(urlencode "$object_name") | |
local object_url="$CUSTOM_CDN_URL/$encoded_object_name" | |
echo "Object URL: $object_url" | |
else | |
echo -e "\n${RED}✗ Upload failed.${NC}" | |
echo "Please check:" | |
echo "- Your AWS credentials are correctly configured" | |
echo "- You have proper permissions on the bucket" | |
echo "- The bucket exists and is accessible" | |
exit 1 | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment