Move and Overwrite File
AnsweredI have a folder A, which a user has put a new file into. My Javabatch program takes that file, manipulates it, and moves it into folder B. That works great.
A user then puts a new version of the same file into folder A. My program then takes that file, manipulates it, and tries to move it into folder B, which fails. Error code is 409, "A resource with this value already exists". That makes sense, because as I said, they specifically put the file into folder A with the same name as the last file they put in for processing, and was already moved to folder B.
What I need to know is, is it possible to move this file from folder A to folder B, overwriting the old file in the process. I don't care if the system counts this as a new file, or a new version of the old file. My main point is when the file is in folder A, I grab the file's ID and shared link and put it somewhere else in a database. And I need to make sure that the link and ID are maintained when the new file goes from folder A to folder B. If an overwrite isn't possible, what's the best way to delete the file in folder B before moving the new file from folder A to folder B?
-
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 twicetry {
//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 -
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.
-
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
Please sign in to leave a comment.
Comments
3 comments