<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic API File Search in Dropbox API Support &amp; Feedback</title>
    <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/API-File-Search/m-p/676845#M30492</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have written this code out in many different ways.&amp;nbsp; I FINALLY got it to work within the limit rates, but now my search is not returning matches.&amp;nbsp; I am just trying to find ANY image filename in the entire account that includes a sku from the database.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code runs fine, just does not find any matches that I know exists in the account.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any advice helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;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) =&amp;gt; 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) =&amp;gt; {
        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(() =&amp;gt; processSKU(sku));
  }
}

run();&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 14 Apr 2023 17:25:13 GMT</pubDate>
    <dc:creator>edodgen</dc:creator>
    <dc:date>2023-04-14T17:25:13Z</dc:date>
    <item>
      <title>API File Search</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/API-File-Search/m-p/676845#M30492</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have written this code out in many different ways.&amp;nbsp; I FINALLY got it to work within the limit rates, but now my search is not returning matches.&amp;nbsp; I am just trying to find ANY image filename in the entire account that includes a sku from the database.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code runs fine, just does not find any matches that I know exists in the account.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any advice helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;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) =&amp;gt; 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) =&amp;gt; {
        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(() =&amp;gt; processSKU(sku));
  }
}

run();&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2023 17:25:13 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/API-File-Search/m-p/676845#M30492</guid>
      <dc:creator>edodgen</dc:creator>
      <dc:date>2023-04-14T17:25:13Z</dc:date>
    </item>
    <item>
      <title>Re: API File Search</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/API-File-Search/m-p/676858#M30495</link>
      <description>&lt;P&gt;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 &lt;A href="https://www.dropbox.com/developers/contact" target="_blank"&gt;open a ticket here&lt;/A&gt; 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.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2023 17:42:39 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/API-File-Search/m-p/676858#M30495</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2023-04-14T17:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: API File Search</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/API-File-Search/m-p/676876#M30496</link>
      <description>&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1677394"&gt;@edodgen&lt;/a&gt; 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 &lt;A href="https://github.com/dropbox/dropbox-sdk-js/blob/main/UPGRADING.md#4-updating-the-response-object" target="_blank" rel="noopener"&gt;using v6 or greater, you should instead&lt;/A&gt; access the matches as 'response.result.matches'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I notice you're using parameters from &lt;A href="https://dropbox.github.io/dropbox-sdk-js/global.html#FilesSearchArg" target="_blank" rel="noopener"&gt;FilesSearchArg&lt;/A&gt; which is meant for &lt;A href="https://dropbox.github.io/dropbox-sdk-js/Dropbox.html#filesSearch__anchor" target="_blank" rel="noopener"&gt;filesSearch&lt;/A&gt;; for the newer &lt;A href="https://dropbox.github.io/dropbox-sdk-js/Dropbox.html#filesSearchV2__anchor" target="_blank" rel="noopener"&gt;filesSearchV2&lt;/A&gt; you should be using &lt;A href="https://dropbox.github.io/dropbox-sdk-js/global.html#FilesSearchV2Arg" target="_blank" rel="noopener"&gt;FilesSearchV2Arg&lt;/A&gt; instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please try those changes and let us know if that helps.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2023 19:08:18 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/API-File-Search/m-p/676876#M30496</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2023-04-14T19:08:18Z</dc:date>
    </item>
  </channel>
</rss>

