8

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?

user2429938
  • 83
  • 1
  • 4

5 Answers5

9
  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.
aebmad
  • 1,390
  • 1
  • 15
  • 14
John Hoerr
  • 7,955
  • 2
  • 30
  • 40
  • I sadly think you're correct. I have to go with option 2 and scan all a folder's child items to see if the file exists manually. Do you or anybody else know if the Box v2 API will make this task easier in the future, or whether it will catch up with the v1 overwrite capability? Thanks. – user2429938 May 29 '13 at 06:40
  • I don't think the Box API ever supported fetching or interacting with an item by path alone. You always had to scan the folder tree to get the item's ID. Once you have that ID you can assume the file exists and overwrite it with the *Upload a New Version of a File* method. – John Hoerr May 29 '13 at 15:45
  • Box API v1 did not support interacting by path alone. But it did support overwriting when uploading a file. Therefore checking to see if a file already exists was entirely optional. It would be great if the v2 API had not lost this feature. The docs refer to the v1 overwrite capability here: http://developers.box.net/w/page/12923951/ApiFunction_Upload%20and%20Download – user2429938 May 29 '13 at 15:48
  • Updated, @McVitas. Thanks for the note. :) – John Hoerr Aug 02 '19 at 14:41
4

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

Spudley
  • 166,037
  • 39
  • 233
  • 307
Julian
  • 59
  • 1
  • 4
1

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.

Ayush
  • 41,754
  • 51
  • 164
  • 239
  • According to the docs, the download file suggestion needs me to know the file ID. I just know the parent folder ID and the file name. – user2429938 May 28 '13 at 21:07
0

Currently you can search for the file instead of scanning the folder tree using the search endpoint:

http://developers.box.com/docs/#search

Within the results, you can compare the name or id of the parent folder you're looking in.

geekly
  • 588
  • 6
  • 10
  • Also, if file found, use the id to upload a new version of the file http://developers.box.com/docs/#files-upload-a-new-version-of-a-file if file is not found, upload a new file http://developers.box.com/docs/#files-upload-a-file – geekly Dec 09 '13 at 20:16
0

For python folks, can use the below code.

def upload_file_to_box(client, folder_id, filename):
        folder = client.folder(folder_id=folder_id)
        items = folder.get_items()
        for item in items:
            if item.name == filename:
                updated_file = client.file(item.id).update_contents(item.name)
                print('File "{0}" has been updated'.format(updated_file.name))
                return
        uploaded_file = folder.upload(filename)
        print('File "{0}" has been uploaded'.format(uploaded_file.name))

This will check for a specific file name and compare it with all files names in the folder and updates a new version if exists, otherwise uploads a new file.

Also you can search the filename inside a folder using search API by using the below code. But the search API has a time lag of 10 minutes or greater.

items = client.search().query(query='"{}"'.format(filename), limit=100, ancestor_folders=[folder])
    
Lijo Abraham
  • 841
  • 8
  • 30