https://discord.cloudflare.com logo
Listing more than 1k KV keys
l

lokiwind

05/25/2023, 9:05 AM
Hello, can this code list more than 1k data that I saved to KV storage at the same time? NOTE:The code currently lists the data without problems, but I wondered if it lists all data when there is more than 1k data
javascript
export default {
    async fetch(request, env, ctx) {
      const keys = new Set();
      let cursor = undefined;
  
      async function gatherKeys() {
        while (true) {
          const result = await env.LOG_IP.list({ cursor });
          for (const { name } of result.keys) {
            keys.add(name);
          }
          if (result.list_complete) {
            break;
          }
          cursor = result.cursor;
        }
      }
  
      await gatherKeys();
  
      const keysArray = Array.from(keys.values());
      const keysText = keysArray.join('\n');
  
      return new Response(keysText, {
        status: 200,
        headers: { 'Content-Type': 'text/plain' },
      });
    },
  };
n

navaru

05/25/2023, 8:22 PM
There is a 128 MB memory limit per worker. If the responses don't come close to that limit, should be fine to return large lists.
l

lokiwind

05/26/2023, 8:10 PM
thank you