<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Bash Upload Script in Dropbox API Support &amp; Feedback</title>
    <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Bash-Upload-Script/m-p/629775#M29088</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to create a script to for each over a folder, and upload to the API but I cannot figure out where I'm going wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the script I have so far, any help would be appreciated &lt;img class="lia-deferred-image lia-image-emoji" src="https://www.dropboxforum.com/html/@FBF7D2AB59A0D6E861EBF6A36F93B7E2/emoticons/1f642.png" alt=":slightly_smiling_face:" title=":slightly_smiling_face:" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#!/bin/bash
UPLOAD_PATH="Backups"

if [[ -f '/root/dropbox.cfg' ]]; then
    echo 'It appears you have set an API Key for Dropbox already.'
    echo 'Using your configured API Key to proceed...'
    source /root/dropbox.cfg
else
    echo 'No existing API Key for Dropbox was found...'
    read -p 'Please provide an API Key to proceed ' api_key
    if [[ -z "$api_key" ]]; then
        echo "No key was provided. Exiting..."
        exit
    fi
    echo "DROPBOX_API_KEY=\"$api_key\"" &amp;gt;&amp;gt; /root/dropbox.cfg
    DROPBOX_API_KEY=$api_key
fi

read -p 'Please provide the path to the folder you wish to upload ' dest_path
if [[ -z "$dest_path" ]]; then
    echo "No path was provided. Exiting..."
    exit
else
    # Check if upload session exists
    if [[ -f "$dest_path/uploadsession.cfg" ]]; then
        echo "File exists using this upload session."
        source "$dest_path/uploadsession.cfg"
    # If it doesn't exist... create a session to be used
    else
        cd $dest_path
        touch uploadsession.cfg
        FIRSTFILE="$dest_path/uploadsession.cfg"
        curl -X POST https://content.dropboxapi.com/2/files/upload_session/start \
            --header "Authorization: Bearer $DROPBOX_API_KEY" \
            --header "Dropbox-API-Arg: {\"close\":false}" \
            --header "Content-Type: application/octet-stream" \
            --data-binary @${FIRSTFILE} &amp;gt;&amp;gt; /root/output.txt

        result=$(grep -oP '(?&amp;lt;="session_id": ")[^"]*' /root/output.txt)

        SESSION_ID=""
        IFS=':'
        for x in $result
        do 
            if [[ $x != "pid_upload_session" ]]; then
                SESSION_ID=$x
            fi
        done
        unset IFS

        echo "SESSION_ID=\"$SESSION_ID\"" &amp;gt;&amp;gt; "$dest_path/uploadsession.cfg"
        echo "Session ID = $SESSION_ID"
    fi

    # For each through each directory and folder, append to session
    for file in $dest_path/* $dest_path/**/*; do
        echo ${SESSION_ID};
        echo $file;
        curl -X POST https://content.dropboxapi.com/2/files/upload_session/append_v2 \
            --header "Authorization: Bearer $DROPBOX_API_KEY" \
            --header "Dropbox-API-Arg: {\"close\":false,\"cursor\":{\"offset\":0,\"session_id\":\"${SESSION_ID}\"}}" \
            --header "Content-Type: application/octet-stream" \
            --data-binary @${file} &amp;gt;&amp;gt; /root/output.txt
    done;

    # Finish Session
    curl -X POST https://api.dropboxapi.com/2/files/upload_session/finish_batch_v2 \
        --header "Authorization: Bearer $DROPBOX_API_KEY" \
        --header "Content-Type: application/json" \
        --data "{\"entries\":[{\"commit\":{\"autorename\":true,\"mode\":\"add\",\"mute\":false,\"path\":\"/${UPLOAD_PATH}\",\"strict_conflict\":false},\"cursor\":{\"offset\":0,\"session_id\":\"${SESSION_ID}\"}}]}"
fi&amp;lt;br /&amp;gt;&lt;/LI-CODE&gt;&lt;/LI-SPOILER&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 17 Oct 2022 15:37:01 GMT</pubDate>
    <dc:creator>ThatBritishOne</dc:creator>
    <dc:date>2022-10-17T15:37:01Z</dc:date>
    <item>
      <title>Bash Upload Script</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Bash-Upload-Script/m-p/629775#M29088</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to create a script to for each over a folder, and upload to the API but I cannot figure out where I'm going wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the script I have so far, any help would be appreciated &lt;img class="lia-deferred-image lia-image-emoji" src="https://www.dropboxforum.com/html/@FBF7D2AB59A0D6E861EBF6A36F93B7E2/emoticons/1f642.png" alt=":slightly_smiling_face:" title=":slightly_smiling_face:" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#!/bin/bash
UPLOAD_PATH="Backups"

if [[ -f '/root/dropbox.cfg' ]]; then
    echo 'It appears you have set an API Key for Dropbox already.'
    echo 'Using your configured API Key to proceed...'
    source /root/dropbox.cfg
else
    echo 'No existing API Key for Dropbox was found...'
    read -p 'Please provide an API Key to proceed ' api_key
    if [[ -z "$api_key" ]]; then
        echo "No key was provided. Exiting..."
        exit
    fi
    echo "DROPBOX_API_KEY=\"$api_key\"" &amp;gt;&amp;gt; /root/dropbox.cfg
    DROPBOX_API_KEY=$api_key
fi

read -p 'Please provide the path to the folder you wish to upload ' dest_path
if [[ -z "$dest_path" ]]; then
    echo "No path was provided. Exiting..."
    exit
else
    # Check if upload session exists
    if [[ -f "$dest_path/uploadsession.cfg" ]]; then
        echo "File exists using this upload session."
        source "$dest_path/uploadsession.cfg"
    # If it doesn't exist... create a session to be used
    else
        cd $dest_path
        touch uploadsession.cfg
        FIRSTFILE="$dest_path/uploadsession.cfg"
        curl -X POST https://content.dropboxapi.com/2/files/upload_session/start \
            --header "Authorization: Bearer $DROPBOX_API_KEY" \
            --header "Dropbox-API-Arg: {\"close\":false}" \
            --header "Content-Type: application/octet-stream" \
            --data-binary @${FIRSTFILE} &amp;gt;&amp;gt; /root/output.txt

        result=$(grep -oP '(?&amp;lt;="session_id": ")[^"]*' /root/output.txt)

        SESSION_ID=""
        IFS=':'
        for x in $result
        do 
            if [[ $x != "pid_upload_session" ]]; then
                SESSION_ID=$x
            fi
        done
        unset IFS

        echo "SESSION_ID=\"$SESSION_ID\"" &amp;gt;&amp;gt; "$dest_path/uploadsession.cfg"
        echo "Session ID = $SESSION_ID"
    fi

    # For each through each directory and folder, append to session
    for file in $dest_path/* $dest_path/**/*; do
        echo ${SESSION_ID};
        echo $file;
        curl -X POST https://content.dropboxapi.com/2/files/upload_session/append_v2 \
            --header "Authorization: Bearer $DROPBOX_API_KEY" \
            --header "Dropbox-API-Arg: {\"close\":false,\"cursor\":{\"offset\":0,\"session_id\":\"${SESSION_ID}\"}}" \
            --header "Content-Type: application/octet-stream" \
            --data-binary @${file} &amp;gt;&amp;gt; /root/output.txt
    done;

    # Finish Session
    curl -X POST https://api.dropboxapi.com/2/files/upload_session/finish_batch_v2 \
        --header "Authorization: Bearer $DROPBOX_API_KEY" \
        --header "Content-Type: application/json" \
        --data "{\"entries\":[{\"commit\":{\"autorename\":true,\"mode\":\"add\",\"mute\":false,\"path\":\"/${UPLOAD_PATH}\",\"strict_conflict\":false},\"cursor\":{\"offset\":0,\"session_id\":\"${SESSION_ID}\"}}]}"
fi&amp;lt;br /&amp;gt;&lt;/LI-CODE&gt;&lt;/LI-SPOILER&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Oct 2022 15:37:01 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Bash-Upload-Script/m-p/629775#M29088</guid>
      <dc:creator>ThatBritishOne</dc:creator>
      <dc:date>2022-10-17T15:37:01Z</dc:date>
    </item>
    <item>
      <title>Re: Bash Upload Script</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Bash-Upload-Script/m-p/629781#M29089</link>
      <description>&lt;P&gt;Hi &lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1583155"&gt;@ThatBritishOne&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Seems you cannot understand the idea of different types of upload strategies. You can use a single call upload (seems most suitable for you) and using &lt;A title="Create a new file with the contents provided in the request" href="https://www.dropbox.com/developers/documentation/http/documentation#files-upload" target="_blank" rel="noopener"&gt;/2/files/upload&lt;/A&gt; you can upload all files, one by one. If some file is bigger than 150MB, then you have to use upload session. Upload session (&lt;A title="Upload sessions allow you to upload a single file in one or more requests" href="https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start" target="_blank" rel="noopener"&gt;/2/files/upload_session/start&lt;/A&gt;) is NOT for upload multiple files, but upload one file in pieces, every one piece 150MB at most. Be careful here. &lt;img class="lia-deferred-image lia-image-emoji" src="https://www.dropboxforum.com/html/@FBF7D2AB59A0D6E861EBF6A36F93B7E2/emoticons/1f642.png" alt=":slightly_smiling_face:" title=":slightly_smiling_face:" /&gt; Seems you are mixing the idea here! Other option is upload multiple files in batch (&lt;A title="This route starts batch of upload_sessions" href="https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start_batch" target="_blank" rel="noopener"&gt;/2/files/upload_session/start_batch&lt;/A&gt;) that let you finish multiple files at once and decrease chance for call conflicts (possible on high load - multiple applications try access/change the same account content simultaneously). Hope this gives direction at least. Something you might be source of mistake is your 'DROPBOX_API_KEY'. What this actually means???!!! The name suppose application key, but you are using it as an access token! Take care. Take in mind that access token need refresh and you didn't implement any handling with refresh token.&amp;nbsp;&lt;img class="lia-deferred-image lia-image-emoji" src="https://www.dropboxforum.com/html/@41457EF40051AFF130FDBFE21B496926/emoticons/1f609.png" alt=":winking_face:" title=":winking_face:" /&gt; You may consider such option.&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Oct 2022 16:15:54 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Bash-Upload-Script/m-p/629781#M29089</guid>
      <dc:creator>Здравко</dc:creator>
      <dc:date>2022-10-17T16:15:54Z</dc:date>
    </item>
    <item>
      <title>Re: Bash Upload Script</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Bash-Upload-Script/m-p/631074#M29145</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the help, I managed to update the script so it for eaches over every file and uploads it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem I'm having now is keeping the folder structure, as it uploads it as a file rather than creates a new folder each time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way I can preserve the path to upload and create the folders automatically?&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2022 20:07:13 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Bash-Upload-Script/m-p/631074#M29145</guid>
      <dc:creator>ThatBritishOne</dc:creator>
      <dc:date>2022-10-22T20:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: Bash Upload Script</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Bash-Upload-Script/m-p/631075#M29146</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1583155"&gt;@ThatBritishOne&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;Is there a way I can preserve the path to upload and create the folders automatically?&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;The API creates folder structure automatically - no folder(s) containing file need creation explicitly (only empty folders, if any, need explicit creation). Check what 'path' have you set for each entry (need to match actual relative path and the Dropbox &lt;A title="Request and response formats - Path formats" href="https://www.dropbox.com/developers/documentation/http/documentation#path-formats" target="_blank" rel="noopener"&gt;path format&lt;/A&gt; rule).&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2022 20:29:02 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Bash-Upload-Script/m-p/631075#M29146</guid>
      <dc:creator>Здравко</dc:creator>
      <dc:date>2022-10-22T20:29:02Z</dc:date>
    </item>
    <item>
      <title>Re: Bash Upload Script</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Bash-Upload-Script/m-p/631339#M29156</link>
      <description>&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1583155"&gt;@ThatBritishOne&lt;/a&gt;&amp;nbsp;Здравко is correct, you can control the folder hierarchy where you upload a file by using the "path" value. For example, if you want to upload a file named "newbackup" into a folder named "10-24-2022" which is inside a folder named "Backups", the "path" value should be "/Backups/10-24-2022/newbackup".&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2022 15:47:45 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Bash-Upload-Script/m-p/631339#M29156</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2022-10-24T15:47:45Z</dc:date>
    </item>
  </channel>
</rss>

