// Example script for downloading recording files
const plivo = require('plivo');
const axios = require('axios');
const { S3Client } = require('@aws-sdk/client-s3');
const { Upload } = require('@aws-sdk/lib-storage');
// ======= CONFIG =======
const AUTH_ID = process.env.PLIVO_AUTH_ID || "<auth_id>";
const AUTH_TOKEN = process.env.PLIVO_AUTH_TOKEN || "<auth_token>";
const AWS_REGION = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || "ap-south-1";
const S3_BUCKET = process.env.S3_BUCKET || "your-bucket";
const S3_PREFIX = process.env.S3_PREFIX || "plivo/recordings"; // optional folder prefix
const USE_SSE = process.env.S3_SSE !== "false"; // AES256 by default
// ======================
(async function main() {
'use strict';
const client = new plivo.Client(AUTH_ID, AUTH_TOKEN);
const s3 = new S3Client({
region: AWS_REGION,
// (Optional) customize retries/timeouts if you want:
// requestHandler: new NodeHttpHandler({ connectionTimeout: 30000, socketTimeout: 120000 })
});
try {
const recordings = await client.recordings.list({
add_time__gt: "2023-04-01 00:00:00",
add_time__lt: "2023-04-30 00:00:00",
offset: 0,
limit: 5,
});
console.log(`Found ${recordings.length} recordings.`);
for (const rec of recordings) {
const recordingUrl = rec.recordingUrl;
const recordingId = rec.recordingId;
const format = rec.recordingFormat || 'mp3'; // fallback
const key = `${S3_PREFIX}/${recordingId}.${format}`;
console.log(`Uploading ${recordingUrl} -> s3://${S3_BUCKET}/${key}`);
try {
// Stream download from Plivo
const resp = await axios({
url: recordingUrl,
method: 'GET',
responseType: 'stream',
timeout: 60_000,
// If the URL requires auth headers/cookies, add here
});
const contentType = resp.headers['content-type'] || 'application/octet-stream';
const contentDisposition = resp.headers['content-disposition'];
// Multipart upload to S3 (handles large files & retries)
const uploader = new Upload({
client: s3,
params: {
Bucket: S3_BUCKET,
Key: key,
Body: resp.data, // streaming body
ContentType: contentType,
...(contentDisposition ? { ContentDisposition: contentDisposition } : {}),
...(USE_SSE ? { ServerSideEncryption: 'AES256' } : {}),
StorageClass: 'STANDARD',
},
// Optional concurrency/tuning:
queueSize: 4, // concurrent parts
partSize: 8 * 1024 * 1024, // 8MB
leavePartsOnError: false,
});
await uploader.done();
console.log(`Uploaded: s3://${S3_BUCKET}/${key}`);
} catch (err) {
console.error(`Failed for ${recordingId}: ${err.message}`);
}
}
} catch (err) {
console.error(err);
process.exit(1);
}
})();