Curl/Bash Sample: File Upload and Download

This example shows how to use the FileCloud API with Curl/Bash to create a folder and upload a file.

#!/bin/bash
# Note: this is an example script, it does not have any error handling.
 
URL='https://filecloud.example.com'
USER=user1
PASSWORD=password4user

cookieFile=${USER}_cookie.txt 
timestamp=`date +%Y-%m-%d_%H-%M-%S`
 
# user login
#-----------
 
echo ">>> loggin in as user $USER"
 
endpoint='/core/loginguest'
urlParams='time='`date +%s`        # optional parameter, simplifies troubleshooting in the server log
 
postData=""
postData+='userid='$USER
postData+='&password='$PASSWORD
 
curl "${URL}${endpoint}?$urlParams"  --cookie-jar $cookieFile  --data-raw "$postData" --compressed
echo
echo "<<<"
 
# create folder
#--------------
 
# set a name for the new folder
FOLDER="newExampleFolder_$timestamp"
 
echo ">>> creating folder $FOLDER for $USER:"
 
endpoint='/core/createfolder'
urlParams='time='`date +%s`        # optional parameter, simplifies troubleshooting in the server log
 
postData=""
postData+="name=$FOLDER"
postData+="&path=/$USER"
 
curl "${URL}${endpoint}?$urlParams"  --cookie $cookieFile  --data-raw "$postData" --compressed
echo
echo "<<<"
 
# upload file to the new folder
#------------------------------
 
UPLOADFILE="newExampleFile_$timestamp.txt"
UPLOADPATH="/${USER}/${FOLDER}"
 
# create a sample file for upload
date > $UPLOADFILE
 
echo ">>> upload file $UPLOADFILE to folder $FOLDER for $USER"
 
endpoint='/upload'
 
urlParams='time='`date +%s`        # optional parameter, simplifies troubleshooting in the server log
urlParams+='&appname=explorer'
urlParams+="&path=$UPLOADPATH"
urlParams+='&offset=0'
urlParams+='&complete=1'
urlParams+="&filename=$UPLOADFILE" 
 
curl -k -X POST -F 'image=@'$UPLOADFILE "${URL}${endpoint}?$urlParams"  --cookie $cookieFile 

# download the file
#------------------------------
 
filepath="/${USER}/${FOLDER}/$UPLOADFILE"
filename=$UPLOADFILE
 
echo ">>> downloading file $filepath"
 
endpoint='/core/downloadfile'
urlParams='time='`date +%s`        # optional parameter, simplifies troubleshooting in the server log
 
postData=""
postData+="filepath=$filepath"
postData+="&filename=$filename"
 
curl "${URL}${endpoint}?$urlParams"  --cookie $cookieFile  --data-raw "$postData" --output ${filename}.downloaded
echo
echo "<<<"