We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.

Forum Discussion

tiamat's avatar
tiamat
Helpful | Level 6
4 years ago

no longer able to upload video via api

The following code once successfully uploaded video, but now does not. No errors thrown on execution of the upload() function and I get 200 a response back from the server for each chunk read from the file. I wonder if there is something wrong with my account - I got a warning to renew and afterwards I started having issues?  Thanks much for your help.

 

const fs = require('fs');
const path = require('path');
const { dbx } = require('../dropbox/dbx');
const MAX_RETRIES = 100;
const CHUNK_SIZE = 10485760;

const uploadBigFile = async (localPath, uploadPath, size) => {
  const stream = fs.createReadStream(localPath, {
    highWaterMark: CHUNK_SIZE,
  });
  let chunkNumber = 0;
  let offset = 0;
  let session_id;

  for await (let chunk of stream) {
    if (!chunkNumber++) {
      const { result } = await dbx.filesUploadSessionStart({
        contents: chunk,
      });
      session_id = result.session_id;
    } else if (offset + chunk.length === size) {
      await dbx.filesUploadSessionFinish({
        contents: chunk,
        cursor: { session_id, offset },
        commit: {
          path: uploadPath,
          mode: 'overwrite',
          client_modified: date,
        },
      });
    } else {
      await dbx.filesUploadSessionAppendV2({
        contents: chunk,
        cursor: { offset, session_id },
        close: false,
      });
    }
    offset += chunk.length;
  }
};

const upload = ({ futureDropboxFileName, date }) => {
  return new Promise(async (resolve, reject) => {
    async function doUpload(futureDropboxFileName, date, photo, retries = 0) {
      const dropboxPath = `${process.env.DROPBOX_FOLDER}${futureDropboxFileName}`;
      const convertedPath = path.join(__dirname, '/tmp/', futureDropboxFileName);
      
      try {
        const { size } = fs.statSync(convertedPath);
        await uploadBigFile(convertedPath, dropboxPath, date, size);
        resolve('ok');
      } catch (error) {
        if (retries <= MAX_RETRIES) {
          doUpload(futureDropboxFileName, date, photo, retries + 1);
        } else {
          return reject(error);
        }
      }
    }
    return doUpload(futureDropboxFileName, date, photo);
  });
};

 

 

  • Thanks, you clued me into a bug that got into my code. 🙂

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    Can you print out the result of filesUploadSessionFinish? If no exceptions are being thrown, that should be returning the metadata of the uploaded file.

    • tiamat's avatar
      tiamat
      Helpful | Level 6

      Thanks, you clued me into a bug that got into my code. 🙂

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,877 PostsLatest Activity: 8 hours ago
325 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!