Skip to content

Instantly share code, notes, and snippets.

@montrealist
Last active December 19, 2017 19:10
Show Gist options
  • Save montrealist/ce1b773742dbe6fed1ab653700c94df6 to your computer and use it in GitHub Desktop.
Save montrealist/ce1b773742dbe6fed1ab653700c94df6 to your computer and use it in GitHub Desktop.
box-node-sdk: search for file first and if it exists upload new version
const fileName = 'something.txt';
client.search.query (
fileName, { limit: 1 },
function(error, data) {
if (
data.total_count &&
data.total_count > 0 &&
data.entries[0].name === fileName
) {
// have a match - upload new version
client.files.uploadNewFileVersion(
data.entries[0].id,
stream,
function(error, data) { /* callback */ }
);
} else {
// no match - upload new file
client.files.preflightUploadFile(
id,
{ name: fileName },
null,
function(error) {
client.files.uploadFile(
id,
fileName,
stream,
function(error, data) { /* callback */ }
);
}
);
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment