cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Discuss Dropbox Developer & API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to know the offset for filesUploadSessionFinishBatch()

How to know the offset for filesUploadSessionFinishBatch()

zainulabd786
Explorer | Level 3

Hi, I am trying to upload many files using the JavaScript SDK's 

filesUploadSessionFinishBatch(),
 
For that, I am looping through all the files and calling 
filesUploadSessionStart() for each file.
 
Things are fine up to this point, but I am wondering How am I supposed to know the value of cursor.offset before getting the lookup_failed error.
 
Here's my code
const uploadImagesToDropbox = async (team_member_id, dbxUser, clientFolder) => {
	try{
		const queueData = await uploadQueue.find({team_member_id, status: 2})
		const appDir = path.dirname(require.main.filename);
		const printResPath = `${clientFolder}/Print Resolution`;
		const webResPath = `${clientFolder}/Web Resolution`;
		const isPrintResPathExists = await dbxFolderExists(dbxUser, printResPath);
		const isWebResPathExists = await dbxFolderExists(dbxUser, webResPath);
		!isPrintResPathExists && await dbxUser.filesCreateFolderV2({path: printResPath})
		!isWebResPathExists && await dbxUser.filesCreateFolderV2({path: webResPath});
		const batchData= [];
		for(let data of queueData){
			let PrintResFilePath = `${appDir}${data.destination.split("/").join("\\")}\\${data.filename}`;
			let WebResFilePath = `${appDir}${data.destination.split("/").join("\\")}\\Web_Resolution\\${data.filename}`;
			let PrintResFile = fs.readFileSync(PrintResFilePath)
			let WebResFile = fs.readFileSync(WebResFilePath)
			const printResStart = await dbxUser.filesUploadSessionStart({ contents: PrintResFile, close: true})
			const webResStart = await dbxUser.filesUploadSessionStart({ contents: WebResFile, close: true})
			batchData.push({
				cursor:{
					session_id: printResStart.session_id,
					offset: 0 //<--- What should I pass Here?
				},
				commit: {
					path: printResPath
				}
			})

			batchData.push({
				cursor:{
					session_id: webResStart.session_id,
					offset: 0 //<--- What should I pass Here?
				},
				commit: {
					path: webResPath
				}
			})
			console.log(data.filename, "Uploaded")
		}console.log(batchData)
		const res = await dbxUser.filesUploadSessionFinishBatch({ entries: batchData });
		console.log(res)
		const status = await dbxUser.filesUploadSessionFinishBatchCheck({async_job_id: res.async_job_id})
		console.log(JSON.stringify(status))
		let interval = setInterval(async () => {
			const status = await dbxUser.filesUploadSessionFinishBatchCheck({async_job_id: res.async_job_id})
			status['.tag'] === "complete" && clearInterval(interval)
			console.log(JSON.stringify(status))
		}, 10000);
		
		await uploadQueue.deleteMany({team_member_id})
		rimraf.sync(`${appDir}\\uploads\\${team_member_id.replace(":", "_")}`)
	} catch(error){
		console.log(error)
		return error;
	}
}
Currently, this code throws the following error
{
  ".tag": "complete",
  "entries": [
    {
      ".tag": "failure",
      "failure": {
        ".tag": "lookup_failed",
        "lookup_failed": {
          ".tag": "incorrect_offset",
          "correct_offset": 11721555
        }
      }
    },
    {
      ".tag": "failure",
      "failure": {
        ".tag": "lookup_failed",
        "lookup_failed": {
          ".tag": "incorrect_offset",
          "correct_offset": 3529761
        }
      }
    },
    {
      ".tag": "failure",
      "failure": {
        ".tag": "lookup_failed",
        "lookup_failed": {
          ".tag": "incorrect_offset",
          "correct_offset": 8843127
        }
      }
    },
    {
      ".tag": "failure",
      "failure": {
        ".tag": "lookup_failed",
        "lookup_failed": {
          ".tag": "incorrect_offset",
          "correct_offset": 2877230
        }
      }
    },
    {
      ".tag": "failure",
      "failure": {
        ".tag": "lookup_failed",
        "lookup_failed": {
          ".tag": "incorrect_offset",
          "correct_offset": 10066923
        }
      }
    },
    {
      ".tag": "failure",
      "failure": {
        ".tag": "lookup_failed",
        "lookup_failed": {
          ".tag": "incorrect_offset",
          "correct_offset": 3037603
        }
      }
    }
  ]
}
I know there's a correct_offset value, but I received it after calling
filesUploadSessionFinishBatchCheck(). However, this is something that I should know before calling the filesUploadSessionFinishBatchCheck() so that I can pass it down
1 Reply 1

Greg-DB
Dropbox Staff

You should keep track of the offset client-side, while uploading the data, so that you can specify the value when sending the next piece of the file, or when finishing the file. There's a sample implementation here.

It looks like your usage is a little different though, in that you're not using filesUploadSessionAppendV2. It sounds like your files are small enough that you can upload all of the data with a single call to filesUploadSessionStart each. In this case, that means that your "offset" would just be the length of the file data.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?