File upload api

Hi there,
reading the documentation here:

How can I specify the folder?

curl -H 'Content-Type: multipart/form-data' \
    -H 'Accept: application/json' \
    -H 'Authorization: Bearer YOUR_API_ACCESS_TOKEN' \
    -X POST \
    -F file=@localfilename' \
    https://api.losant.com/applications/APPLICATION_ID/file/FILE_ID/upload

Hi @Paolo_Proxy,

If you are referring to the local file to upload, you can just add the path on the file= such as file=@./this/path/localfilename.txt.

If you are referring to the file location in the application, you can specify the directory when making the post request. You will need the ID from the result of the post request in order to use the upload endpoint.

curl -H 'Content-Type: application/json' \
    -H 'Accept: application/json' \
    -H 'Authorization: Bearer YOUR_API_ACCESS_TOKEN' \
    -X POST \
    -d '{"name":"example.csv","type":"file","parentDirectory":"/my/file/path","fileSize":500,"contentType":"text/csv"}' \
    https://api.losant.com/applications/APPLICATION_ID/files

If you have an existing file, you can specify the directory by the move action and then use the upload action to replace the file.

I hope this helps. Let me know if you have any other questions.

Erin

Hello Erin,
yes I was referring to the remote location.
I am confused about how to use the two step you mentioned:

  1. when I call the first POST, how do I choose a FILEID? Should I generate one randomly? Where is the file going to appear? in the root folder?
  2. when I then call the second POST, how do I refer to the FILEID? Your example shows the file name but not the FILEID.

Cheers.

Hey @Paolo_Proxy,

When you make the first POST call to https://api.losant.com/applications/APPLICATION_ID/files with a post body, like the following:

{
  "name": "file.csv",
  "type": "file",
  "parentDirectory": "/",
  "fileSize": 500,
  "contentType": "text/csv"
}

where name is the name of the file you would like to use, the parentDirectory is the directory. In this example “/” would be the root directory but you could do “/foo/bar”, and your file would appear in the directory bar under the directory foo e.g. /foo/bar/file.csv. If this request is successful you should be able to see the directory and the file in your application via the UI. The file will say the status is “pending” since no upload has been attempted yet.

if the request is successful you would get a response of a file object as seen in this example or like the following:

{
  "id": "575ec8687ae143cd83dc4a96",
  "applicationId": "575ec8687ae143cd83dc4a97",
  "creationDate": "2016-06-13T04:00:00.000Z",
  "lastUpdated": "2016-06-13T04:00:00.000Z",
  "authorId": "575ed70c7ae143cd83dc4aa9",
  "authorType": "user",
  "status": "pending",
  "name": "file.csv",
  "type": "file",
  "parentDirectory": "/",
  "fileSize": 500,
  "contentType": "text/csv"
}

In this example the “id” field would be the FILEID you use in the second post.

So based on this example if you wanted to use the post upload endpoint.

curl -H 'Content-Type: multipart/form-data' \
    -H 'Accept: application/json' \
    -H 'Authorization: Bearer YOUR_API_ACCESS_TOKEN' \
    -X POST \
    -F file=@localfilename' \
    https://api.losant.com/applications/575ec8687ae143cd83dc4a97/file/575ec8687ae143cd83dc4a96/upload

And if that is successful, then in the UI the status of the files will turn to completed. You will have access to a download link to share or redownload the file to confirm the contents are what you expect.

I hope this helps. Let me know if you need any further explanation.

Best,
Erin

1 Like

The order now makes sense!

@Erin_Thompson any examples using the python client library?

There seems to be an issue:

result = client.file.upload(
applicationId=my_application_id,
fileId=my_file_id,
file=my_file)

print(result)

Gives me this:

AttributeError: ‘File’ object has no attribute ‘upload’

From documentation:

https://github.com/Losant/losant-rest-python/blob/master/docs/file.md#upload