Upload to DropBox

Is there a way to upload files to dropbox via basic script similar to FTP?

@dlabelle

Unfortunately FTPing into Dropbox is not possible however there are alternatives.

It is possible to upload thing using the dropbox API (see example at bottom for live). Though you will need to manually read the files and export them as text body (can’t seem to get actual file POSTING to work with their api). This is something that would need to be done via scripting and cannot be done via a UI.


In this example we will be exporting our instant values to dropbox in a file that has the date and time for when the file was sent.

Dropbox Requirements

While the eWON requirements are minimal, you will need to configure somethings in dropbox as well in order to use their API fully. We will need an API key in order to send files to Dropbox from the BASIC engine.




  1. We first need to create an application within Dropbox. This application will the end storage location for our files. In this example we will be using the general Dropbox API not Dropbox Business Api.

    • API Type: Dropbox API

    • Access Needed: App Folder (Full is unneeded)

    • Name: My eWON App (you select your own name)


  1. After creating your application, you will be taken to the application dashboard where you can configure your application parameters. In our example, we only need to generate our access token (used for authentication). This token is a hard requirement for doing this.

That finalizes the application setup on Dropbox but now lets take a look at what requirements there are in the actual API.

Header Values

Key Value
Authorization Bearer YOURAPIKEY
Dropbox-API-Arg {"path": "Dropbox File Path", "mode": "add", "autorename": true, "mute": false}
Content-Type application/octet-stream

Now in the above header value table, the key value that will come into play later is the Dropbox-API-Arg value. This value actually specifies the path on Dropbox as to where to put the file.

So now that we have reviewed the Dropbox side, lets look at our script.

eWON BASIC Script

The actual exportation of the log file can be triggered based on a tag or really any other threshold you set (ondate, onchange, onerror,etc). In my example, I will be exporting the log file when a tag value ("Trigger") is changed.

Challenges:
I did happen to run into a few snags, the main being I could not upload the raw file but had to read the file and then build a string out of it. I’m certain we can push the file but I could not make it work. If I can I will update the question to reflect that.

INIT Section:
ONCHANGE "Trigger", "GOTO ExportToDropbox"

ExportToDropbox:
ivText$ = ""
$url$ = "https://content.dropboxapi.com/2/files/upload"
$key$ = "YOUR API KEY"
OPEN "exp:$dtIV$ftT" FOR TEXT INPUT AS 1
GOSUB Loop
@uploadToDropbox($url$, ivText$, $key$)
END

//As I previously mentioned, I hit some snags when pushing the full file so I am just building a string instead. 
LOOP:
entry$ = GET 1
IF (entry$<> "") THEN
    ivText$ = ivText$ + CHR$(13) + CHR$(10) + entry$
    GOTO Loop
ENDIF
Close 1
RETURN
END

Now we can finally send the data to dropbox with this function.

FUNCTION uploadToDropbox($dropboxUrl$, $fileData$, $apiKey$)
  t$ = TIME$
  $fileName$ = "eWONInstantValue" + t$(7 To 10)+"-"+T$(4 To 5)+"-"+T$(1 To 2) + "_" + T$(12 TO LEN t$)+ ".txt"
  $dropboxApiArg$ = '{"path": "/ewon/logs/' + $fileName$ + '","mode": "add","autorename": true,"mute": false}'
  $method$ = "POST"
  $headers$ = "Content-Type=application/octet-stream&Authorization=" + $apiKey$
  $headers$ = $headers$ + "&Dropbox-API-Arg=" + $dropboxApiArg$
  REQUESTHTTPX $dropboxUrl$, $method$ , $headers$,$fileData$
ENDFN



End Result



While this is not as straightforward as FTP, this demonstrates that you can in fact export your data to Dropbox with a little work.

2 Likes

hai

can i get file full basic script for upload dropbox?

Thank You

Hello Peter,

The BASIC SCRIPT above should help.

We cannot create custom scripts on here. However, we do offer it as a paid service. You can reach out to our sales team for further inquiries.

can you help me for check this basic script:
Init Section:
ONCHANGE “Trigger”, “GOTO ExportToDropbox”

Dropbox:
FUNCTION uploadToDropbox($Url$, $fileData$, $Key$)
t$ = TIME$
$fileName$ = “eWONInstantValue” + t$(7 To 10)+"-"+T$(4 To 5)+"-"+T$(1 To 2) + “_” + T$(12 TO LEN t$)+ “.txt”
$dropboxApiArg$ = ‘{“path”: "/Apps/MyEwonApp/’ + $fileName$ + ‘",“mode”: “add”,“autorename”: true,“mute”: false}’
$method$ = “POST”
$headers$ = “Content-Type=application/octet-stream&Authorization=” + $Key$
$headers$ = $headers$ + “&Dropbox-API-Arg=” + $dropboxApiArg$
REQUESTHTTPX $Url$, $method$ , $headers$,$fileData$
ENDFN

CreateDropboxString:
ExportToDropbox:
ivText$ = “”
$url$ = “https://content.dropboxapi.com/2/files/upload
$key$ = “szrDJ_FR7WgAAAAAAAAAtIHmwoI9SgDXNW4IgVf7HN-uPV1Pr####3
OPEN “exp:$dtIV$ftT” FOR TEXT INPUT AS 1
GOSUB Loop
@uploadToDropbox($url$, ivText$, $key$)
END
//As I previously mentioned, I hit some snags when pushing the full file so I am just building a string instead.
LOOP:
entry$ = GET 1
IF (entry$<> “”) THEN
ivText$ = ivText$ + CHR$(13) + CHR$(10) + entry$
GOTO Loop
ENDIF
Close 1
RETURN
END