Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BOX v2 API for overwriting a file or checking one exists before trying to upload

Tags:

box-api

I'm new to the BOX API so am using v2 of the API. I am making REST calls natively from my app.

I want to upload a file which may or may not have been previously uploaded. I know the parent folder ID and the file name. I need to either overwrite an existing file or at least make another call to see if the file already exists. I can see no way of doing either with the v2 API.

I can upload files without problem. But of course I get an error if the file already exists.

  1. In v1 there was a way to specify an overwrite in the upload call. No such thing in v2 as far as I can tell. Am I correct? Will an overwrite flag get added back into the API?
  2. Given a file name how do I see if this already exists in a specific parent folder?

To me this is fairly basic stuff so perhaps I am missing something reasonably obvious?

like image 634
user2429938 Avatar asked May 28 '13 19:05

user2429938


3 Answers

  1. You are correct that the Upload a File method will fail if a file with the same name already exists in the folder. If you want to overwrite a known file, consider using the Upload a New Version of a File method.
  2. If you know the ID of the parent folder, you can use the Retrieve a Folder's Items method to get a list of the items in that folder. Each item will have a name property, which you can use for comparison, and an id property, which you can use in conjunction with the Upload a New Version of a File method mentioned above.
like image 178
John Hoerr Avatar answered Sep 21 '22 16:09

John Hoerr


the file id of the file on box has the same name as the one you are uploading is outputted in the error message response to get it simply call $data->context_info->conflicts->id then use that to overwrite the file with Upload a New Version of a File method

like image 40
Julian Avatar answered Sep 24 '22 16:09

Julian


You re correct that in v2 of the API, uploading a file that already exists causes an error. What you can do is check if the file exists before uploading.

When you attempt to download a file, make a cURL call without the 'follow redirects' option. If the response is a 302 (with the actual download link), you know the file exists. If you get a 404, it does not exist.

For the above, it is important to specify that cURL should not follow redirects, otherwise if the file does exist, you will end up downloading it.

like image 40
xbonez Avatar answered Sep 24 '22 16:09

xbonez