Curl/Bash Sample: File Upload

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

rawData=""
rawData+='userid='$USER
rawData+='&password='$PASSWORD

curl "${URL}${endpoint}?$urlParams"  -c $cookieFile  --data-raw "$rawData" --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

rawData=""
rawData+="name=$FOLDER"
rawData+="&path=%2F$USER"

curl "${URL}${endpoint}?$urlParams"  -b $cookieFile  --data-raw "$rawData" --compressed
echo
echo "<<<"

# upload file to the new folder
#------------------------------

UPLOADFILE="newExampleFile_$timestamp.txt"
UPLOADPATH="%2F${USER}%2F${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"  -b $cookieFile 
echo
echo "<<<"