Unknown model· unknown
from NVIDIA HelpSteer3 Preference
write a node program for downloading a large file from S3 with multiple concurrent range…
- User
write a node program for downloading a large file from S3 with multiple concurrent range requests
- Assistant
Here's a Node.js program that uses the AWS SDK to download a large file from Amazon S3 using multiple concurrent range requests. This example uses the
asynclibrary for handling promises andp-queuefor managing concurrent requests. Make sure to install the required packages by runningnpm install aws-sdk async p-queue.javascriptconst AWS = require('aws-sdk'); const async = require('async'); const PQueue = require('p-queue'); // Configure AWS SDK AWS.config.update({ accessKeyId: 'your_access_key_id', secretAccessKey: 'your_secret_access_key', region: 'your_region' }); const s3 = new AWS.S3(); const queue = new PQueue({ concurrency: 5 }); // Adjust concurrency based on your needs async function downloadFileInChunks(bucket, key, localFilePath, chunkSize = 10 * 1024 * 1024) { try { const { Body, ContentLength } = await s3.headObject({ Bucket: bucket, Key: key }).promise(); const numChunks = Math.ceil(ContentLength / chunkSize); const chunks = Array.from({ length: numChunks }, (_, i) => ({ start: i * chunkSize, end: Math.min((i + 1) * chunkSize - 1, ContentLength - 1) })); await async.eachLimit(chunks, 10, async ({ start, end }) => { await queue.add(async () => { const params = { Bucket: bucket, Key: key, Range: `bytes=${start}-${end}` }; const data = await s3.getObject(params).promise(); const buffer = data.Body.read(); await writeChunkToFile(localFilePath, buffer, start); }); }); console.log(`Download completed: ${localFilePath}`); } catch (error) { console.error('Error downloading file:', error); } } function writeChunkToFile(filePath, buffer, start) { return new Promise((resolve, reject) => { const fs = require('fs'); const fd = fs.openSync(filePath, 'a+'); fs.write(fd, buffer, 0, buffer.length, start, (err, bytesWritten) => { if (err) { reject(err); } else { resolve(bytesWritten); } }); }); } (async () => { const bucket = 'your_bucket_name'; const key = 'path/to/your/large/file.ext'; const localFilePath = 'path/to/local/file.ext'; await downloadFileInChunks(bucket, key, localFilePath); })();
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.