Reputation: 173
I have a url for a .wav file. I'd like to save it to an S3 bucket from a Lambda function. Do I have to download it first?
What's the best way to do this?
exports.handler = async (event) => {
// imports
const fs = require('fs');
const AWS = require('aws-sdk');
AWS.config.update({ region: process.env.REGION || 'us-east-1' })
const s3 = new AWS.S3();
// get URL
const body = parseBody(event['body']);
const url = body.url;
// download file?
// HOW TO DO THIS using async?
const file_name = magic_save(url)
// upload to S3
var bucketName = `some_bucket`;
var keyName = 'audio.wav';
const fileContent = fs.readFileSync(file_name);
var params = { 'Bucket': bucketName, 'Key': keyName, 'Body': fileContent };
try {
console.log('saving...');
const data = await s3.putObject(params).promise();
console.log("Successfully saved object to " + bucketName + "/" + keyName);
} catch (err) {
console.log('err');
console.log(err);
};
Upvotes: 1
Views: 591
Reputation: 7770
Best will be to stream the file directly to s3 like this
const got = require("got");
const aws = require("aws-sdk");
const s3Client = new aws.S3();
const Bucket = 'somebucket';
const Key = "some/audio.wav";
exports.handler = async (event) => {
// get URL
const body = parseBody(event['body']);
const url = body.url;
const stream = got.stream(url);
const response = await s3Client.upload({Bucket, Key, Body: stream}).promise();
console.log(response);
};
Upvotes: 2