Level up your Box knowledge with brand new learning paths on Box University. Visit training.box.com to get started

Move and Overwrite File

Answered
New post

Comments

3 comments

  • Peter Christensen

    Hi Josh

    The 409 error actually provides you with the ID of the existing file which will allow you to version this when you get the 409 error. 
    Something like this pseudo code - using the preflight check 'canUpload' prevents you from sending the actual file twice

    try {
    //test upload first
    folderB.canUpload(fileName, fileSize);
    //upload new file to Folder B
    folderB.uploadFile(fileStream, fileName);

    } catch (BoxAPIException e) {

    if(e.getResponseCode()==409) {

    // 409 means file name conflict. The "e.getResponse()" returns a JSON string with detailed info on your error

    //read "context_info.conflicts.id" from the JSON, this will return the ID of the file in Folder B with the same name
    //create new BoxFile object with the "context_info.conflicts.id" from above
    //call BoxFile.uploadNewVersion on the BoxFile object with the new file you wanted in Folder B
    BoxFile existingFile = new BoxFile(api,<<read the context_info.conflicts.id through some JSON lib>>)
    existingFile.uploadNewVersion(fileStream)
    }

    More about preflight check https://github.com/box/box-java-sdk/blob/master/doc/files.md#upload-preflight-check

    Best regards,
    Peter Christensen, Platform Solutions Engineer, Box

    0
    Comment actions Permalink
  • Tibor Tóth

    I have a similar problem, but I DO NEED to keep the versions. However I would not like to upload the files again, rather move or copy from an other folder. Here is my use case:

    Within Box, I am trying to move a file to an other folder, using the Box API PUT command. It works first time, but when a new file arrives to the source folder and want to move that, too, it gives a Error code:409, "Item with the same name already exists".

    Our use case is that files arrive to a specific folder, we are using a Python script (with requests module) to download for further processing and after the download, we would like to move the file to an "archive" folder for later reference within Box - still do it with the Python script. In this archive folder we would like to see "versions" of the files, so in any case could revert or at least look at an earlier version.

    This move step does not work, as the file always have the same name...

    Is there something I miss around the PUT command? I tried POST as well to copy first, but same result.

     

    0
    Comment actions Permalink
  • Peter Christensen

    hi Tibor

    There isn't an endpoint that will allow you to version a file with an existing Box File when you move files around between folders. You will need to use the upload API to upload the file as a new version to your existing file as per the example I provided above when you get the 409 error.

    You can always use https://pulse.box.com to suggest new features and capabilities to Box API. 

    Rgds,
    Peter

    0
    Comment actions Permalink

Please sign in to leave a comment.