Skip to content

Instantly share code, notes, and snippets.

@gary23w
Created April 7, 2023 13:53
Show Gist options
  • Save gary23w/ec583fd45a90808d204cc47d2830104b to your computer and use it in GitHub Desktop.
Save gary23w/ec583fd45a90808d204cc47d2830104b to your computer and use it in GitHub Desktop.
a script to dump an entire directory into pcloud
#!/bin/bash
#################################gary23w-loves-you##########################################
# This is a bash script that uploads files and directories from a specified directory #
# path to a cloud storage service called pCloud. The script uses OAuth2 authorization #
# to obtain an access token, and then uses the pCloud API to upload files and create #
# directories. The variables username and password are not used in this script, #
# but may have been used for a previous version. The variables client_id and client_secret #
# are used for authentication with the pCloud API. The variables #
# main_folder_id, depth, and max_depth are used to define the root folder in pCloud, the #
# current directory depth, and the maximum directory depth, respectively. The functions #
# upload_file and upload_directory are used to upload a single file and a directory, #
# respectively. The script then loops through all files and directories in the #
# specified directory path, and uploads them to pCloud using the appropriate functions. #
############################################################################################
# I needed something quick to upload a bunch of files to pCloud, so I wrote this script.
username=""
password=""
client_id=""
client_secret=""
main_folder_id=""
depth=0
max_depth=10
# Set the path to the directory you want to upload
dir_path="/mnt"
# Get the access token
auth_url="https://my.pcloud.com/oauth2/authorize?client_id=${client_id}&response_type=code"
echo "Please open the following URL in a web browser and authorize the app:"
echo "${auth_url}"
read -p "Enter the authorization code: " auth_code
echo $auth_code
access_token=$(curl -s -X POST "https://api.pcloud.com/oauth2_token" \
-d "client_id=${client_id}" \
-d "client_secret=${client_secret}" \
-d "code=${auth_code}" | jq -r '.access_token')
# Function to upload a single file
function upload_file {
file_path="$1"
folder_path="$2"
file_name="$(basename $file_path)"
echo "Uploading $file_path to pCloud..."
curl -X POST "https://api.pcloud.com/uploadfile?access_token=${access_token}&folderid=${folder_path}" \
-F "file=@${file_path}"
}
function upload_directory {
dir_path="$1"
folder_path="$2"
dir_name="$(basename $dir_path)"
new_directory="${folder_path}/${dir_name}"
echo "Creating directory $new_directory in pCloud..."
folderid=$(curl -X POST "https://api.pcloud.com/createfolder?access_token=${access_token}&folderid=${folder_path}&name=${dir_name}" | jq -r '.metadata.folderid')
#check if there are any files in the directory
if [ "$(ls -A $dir_path)" ]; then
echo "Uploading files in $dir_name to pCloud..."
for file in "$dir_path"/*; do
if [ -f "$file" ]; then
upload_file "$file" "$folderid"
elif [ -d "$file" ]; then
if [ "$depth" -lt "$max_depth" ]; then
((depth++))
upload_directory "$file" "$folderid"
cd ..
((depth--))
else
echo "Max depth of $max_depth reached"
fi
fi
done
elif [ ! "$(ls -A $dir_path)" ]; then
echo "No files in $dir_name"
fi
}
# Upload files and subdirectories in the root directory
for file in "$dir_path"/*; do
if [ -f "$file" ]; then
upload_file "$file" "$main_folder_id"
elif [ -d "$file" ]; then
cd "$file"
upload_directory "$file" "$main_folder_id"
cd ..
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment