cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

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

API File Search

API File Search

edodgen
New member | Level 2

 

I have written this code out in many different ways.  I FINALLY got it to work within the limit rates, but now my search is not returning matches.  I am just trying to find ANY image filename in the entire account that includes a sku from the database.  

 

The code runs fine, just does not find any matches that I know exists in the account.  

 

Any advice helps.

 

const AWS = require('aws-sdk');
const fs = require('fs');
const mysql = require('mysql2/promise');
const path = require('path');
const Dropbox = require('dropbox').Dropbox;
const throttledQueue = require('throttled-queue');

const dbx = new Dropbox({
  accessToken:
    '#',
});

const pool = mysql.createPool({
  connectionLimit: 16,
  host: 'localhost',
  user: 'root',
  password: '#',
  database: 'absdropbox',
});

const throttledRequestsPerSecond = 15;

const throttle = throttledQueue(throttledRequestsPerSecond, 1000);

async function getSKUs() {
  const [rows] = await pool.query('SELECT sku FROM oe');
  return rows.map((row) => row.sku);
}

async function queryDropbox(query) {
  const options = {
    path: '',
    // path: '/home/2022',
    mode: 'filename_and_content',
    query: query,
  };
  try {
    const response = await dbx.filesSearchV2(options);
    return response.matches ?? [];
  } catch (error) {
    console.error(error);
    return [];
  }
}

let i = 0
async function processSKU(sku) {
  // const query = `filename LIKE '%${sku}%'`;
  // const query = `filename LIKE '_${sku}_'`;
  const query = `search('${sku}')`;


  const matches = await queryDropbox(query);

  console.log(i++)
  if (matches.length === 0) {
    console.log(`No matches found for SKU ${sku}`);
    return;
  }

  console.log(`Found ${matches.length} match(es) for SKU ${sku}`);

  for (const match of matches) {
    const { path_display } = match.metadata;
    const filename = path.basename(path_display);

    const fileStream = fs.createWriteStream(filename);

    try {
      const { data } = await dbx.filesDownload({ path: path_display });
      data.pipe(fileStream);
      await new Promise((resolve, reject) => {
        fileStream.on('finish', resolve);
        fileStream.on('error', reject);
      });

      const s3 = new AWS.S3({ region: 'us-east-2' });
      const s3Params = {
        Bucket: 'dbexctract',
        Key: filename,
        Body: fs.createReadStream(filename),
      };
      await s3.upload(s3Params).promise();
      console.log(`File ${filename} uploaded to S3`);
    } catch (error) {
      console.error(error);
    } finally {
      fs.unlinkSync(filename);
    }
  }
}

async function run() {
  const skus = await getSKUs();
  for (const sku of skus) {
    await throttle(() => processSKU(sku));
  }
}

run();

 

 

2 Replies 2

Greg-DB
Dropbox Staff

If I understand, it sounds like you're calling 'filesSearchV2' with a particular 'query' that is expected to match something in the connected account, but 'response.matches' is coming back empty. Is that correct? In that case, it would be best to open a ticket here from the affected account and include the options you are using, including a sample 'query' value, that are unexpectedly not producing results so we can look into it specifically for you.

Greg-DB
Dropbox Staff

@edodgen I was looking through your code, and I see you're using 'response.matches' to access the search matches. What version number of the SDK are you using? If you're using v6 or greater, you should instead access the matches as 'response.result.matches'.

 

Also, I notice you're using parameters from FilesSearchArg which is meant for filesSearch; for the newer filesSearchV2 you should be using FilesSearchV2Arg instead.

 

Please try those changes and let us know if that helps.

Need more support?
Who's talking

Top contributors to this post

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