9 Replies Latest reply on May 18, 2017 3:45 AM by iCLINICIAN

    Insert from URL with Dropbox API

    iCLINICIAN

      With the introduction of cURL options in FM16's Insert from URL script step I've been investigating the possibility of being able to save a copy of my FM solution directly to my Dropbox folder using the https://content.dropboxapi.com/2/files/upload element from Dropbox V2 API.

       

      I've successfully created the access token to pass through as part of this step and the Insert from URL script step successfully creates the stated folder and file in my Dropbox.  However, the file size is always 0 bytes.  I've tried 2 different methods

       

      METHOD 1

      #Export a compacted copy of this file to Temporary Folder

      Set Variable [ $file_path ; Get ( TemporaryPath ) & "Solution.fmp12" ]

      Save a Copy as [ "$file_path" ; compacted ]

       

      #Set my cURL options to variable

      Set Variable [ $curl_header ;

      "curl -X POST https://content.dropboxapi.com/2/files/upload \\" & ¶ &

      "--header \"Authorization: Bearer " & PREFERENCES::Dropbox_AccessToken & "\"  \\" & ¶ &

      "--header \"Dropbox-API-Arg: {\\\"path\\\": \\\"/DB2FM/Solution.fmp12\\\"}\" \\" & ¶ &

      "--header \"Content-Type: application/octet-stream\" \\" & ¶ &

      "--data-binary @" & $file_path ]

       

      #Dropbox URL

      Set Variable [ $dropbox_url ; "https://content.dropboxapi.com/2/files/upload" ]

       

      #Make Request - Set to variable adn then format to JSON field

      Insert from URL [ Verify SSL Certificates ; With dialog: Off ; $json ; $dropbox_url ; cURL options: $curl_header ]

      Set Field [ PREFERENCES:Dropbox_JSON ; JSONFormatElements ( $json ) ]

       

       

       

      METHOD 2

      #Export a compacted copy of this file to Temporary Folder and then import to a container field

      Set Variable [ $file_path ; Get ( TemporaryPath ) & "Solution.fmp12" ] //note1

      Save a Copy as [ "$file_path" ; compacted ]  //note1

      Insert File [ PREFERENCES::Container ; "$file_path" ]  //note1

      #Set variable following instructions on FileMaker website which states:

      #  -  Direct file access is replaced with a FileMaker variable prefixed by the @ character.

      #  -  To access a file, you can set the variable to a container field, which contains the file, then use that variable as the parameter of the cURL option.

      Set Variable [ $container_path ; PREFERENCES::Container ]

       

      #Set my cURL options to variable

      Set Variable [ $curl_header ;

      "curl -X POST https://content.dropboxapi.com/2/files/upload \\" & ¶ &

      "--header \"Authorization: Bearer " & PREFERENCES::Dropbox_AccessToken & "\"  \\" & ¶ &

      "--header \"Dropbox-API-Arg: {\\\"path\\\": \\\"/DB2FM/Solution.fmp12\\\"}\" \\" & ¶ &

      "--header \"Content-Type: application/octet-stream\" \\" & ¶ &

      "--data-binary @" & $container_path ]

       

      #Dropbox URL

      Set Variable [ $dropbox_url ; "https://content.dropboxapi.com/2/files/upload" ]

       

      #Make Request - Set to variable adn then format to JSON field

      Insert from URL [ Verify SSL Certificates ; With dialog: Off ; $json ; $dropbox_url ; cURL options: $curl_header ]

      Set Field [ PREFERENCES:Dropbox_JSON ; JSONFormatElements ( $json ) ]

       

       

       

      JSON Response For Both Methods

       

      I get the following JSON Response for both methods

      {

          "client_modified" : "2017-05-16T11:22:47Z",

          "content_hash" : "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",

          "id" : "id:heJWQ1SWwJsAAAAAAcMWNw",

          "name" : "Solution.fmp12",

          "path_display" : "/DB2FM/Solution.fmp12",

          "path_lower" : "/db2fm/solution.fmp12",

          "rev" : "3a0412f00f58892",

          "server_modified" : "2017-05-16T11:22:48Z",

          "size" : 0

      }

       

      Any ideas as to why the file is created but with no content.  I've also tried this with a number of different file types (.png, .txt, .pdf) by manually inserting a file into the container and greying out the 3 steps marked //note1 above but the result is always the same.

        • 1. Re: Insert from URL with Dropbox API
          mikebeargie

          FWIW - I wouldn't use method #2 for large files, it could crash or timeout with no explanation if used in production.

           

          #1 would only have issues if the permissions of the file in the temp directory were not available for read access to send it to dropbox.

           

          The file upload limit is 150mb for the /upload route if that makes a difference. In that case you should use /upload_session. However that is also a problem since that route expects you to divide the file into 150mb chunks.

           

          Have you verified that the temp directory file has contents? Could it possibly be that it's not saving it correctly.

           

          If the file is saved correctly, directory permissions would be the next thing I checked out.

           

          You may also benefit from using a program like Postman to perform tests with a filemaker file and the API requests to see what works and doesn't for uploading (easier to test there than in filemaker sometimes).

           

          Good luck, if you want to abstract and share your work when complete, I'm sure the community would enjoy the resource.

          • 2. Re: Insert from URL with Dropbox API
            user19752

            Your $file_path may be bad for the curl parameter since Get ( TemporaryPath ) have starting slash.

            If you use Windows, remove it. OSX, you may need adding "/Volumes".

            • 3. Re: Insert from URL with Dropbox API
              iCLINICIAN

              Thanks for the feedback Mike. I'd planned on getting the single upload working first and then see if I could tackle the upload_session method.

               

              I'm confident that the temp folder has content as also used the Get ( DesktopPath ) as a test and the copy of created successfully.  Will have a look at permissions though although I won't have any control over this once it moves on to my iPad.

               

              I'm using OSX so have tried adding "/Volumes" to the start of the path but still no joy.

              • 4. Re: Insert from URL with Dropbox API
                user19752

                Oops. very sorry, I hadn't read the help for the step. You can't set (OS) path there, need variable name, and the content of variable is reference to container field, not filepath.

                So try method 2 with

                "--data-binary @$container_path" ]

                • 5. Re: Insert from URL with Dropbox API
                  user19752

                  And you don't need 1st line "curl " for the option. It is not command line of curl, option for FM script step. (I feel this is confusing like as file path in FM has its original syntax...)

                  I like "httppost:" is removed, -X POST maybe without URL in cURL option?

                  Set Variable [ $curl_header ;

                  "-X POST " &

                  (the option need space, don't need line break)

                  • 6. Re: Insert from URL with Dropbox API
                    iCLINICIAN

                    Thanks for the suggestions.  Unfortunately still no joy.  I've now got:

                     

                    #Container path variable

                    Set Variable [ $container_path ; PREFERENCES::GoZyncMobile_Container ]

                     

                    #cURL actions variable

                    Set Variable [ $curl_header ;

                    "-X POST \\" &

                    "--header \"Authorization: Bearer " & PREFERENCES::Dropbox_AccessToken & "\"  \\" &

                    "--header \"Dropbox-API-Arg: {\\\"path\\\": \\\"/DB2FM/backup.fmp12\\\"}\" \\" &

                    "--header \"Content-Type: application/octet-stream\" \\" &

                    "--data-binary @" & $container_path ]

                     

                    #Dropbox URL variable

                    Set Variable [ $dropbox_url ; "https://content.dropboxapi.com/2/files/upload" ]

                     

                    #Make Reques

                    Insert from URL [ Verify SSL Certificates ; With dialog: Off ; $json ; $dropbox_url ; cURL options: $curl_header ]

                    • 7. Re: Insert from URL with Dropbox API
                      wimdecorte

                      iCLINICIAN wrote:

                       

                      "--data-binary @" & $container_path ]

                       

                       

                       

                      should be:

                      "--data-binary @$container_path"

                       

                      Note that you basically hard code the name of the variable instead of pointing to the content of the variable.

                      1 of 1 people found this helpful
                      • 8. Re: Insert from URL with Dropbox API
                        wimdecorte

                        The fact that you name the variable $container_path may point to some misconception.  You are not pointing to the path of the file that is the container, you actually point to the container content, the actual binary data.

                         

                        The FM cURL options do not allow interaction with the file system itself, for security reasons.

                        • 9. Re: Insert from URL with Dropbox API
                          iCLINICIAN

                          That's done it!  Many thanks to both of you.  Had I read user19752's post correctly I'd have spotted the discrepancy in my code.

                           

                          I'll tidy up my FileMaker code and put it together in a demo file and upload.

                           

                          Many thanks again for your help.