I am currently working on an integration between Hover and my app. I have most of the nuts and bolts figured out but I cannot get the images portion figured out. The specific API I am trying to dial in can be found here:
Image logged out in my Node server:
CodePudding user response:
The solution below worked for me:
const downloadFile = async (downloadUrl: string): Promise<any> => {
return axios.get(downloadUrl, {
responseType: 'stream',
});
};
const uploadFromStream = (
fileResponse: AxiosResponse,
fileName: string,
bucket: string,
): { passThrough: PassThrough; promise: Promise<S3.ManagedUpload.SendData> } => {
const s3 = new S3();
const passThrough = new PassThrough();
const promise = s3
.upload({
Bucket: bucket,
Key: fileName,
ContentType: fileResponse.headers['content-type'],
ContentLength: fileResponse.headers['content-length'],
Body: passThrough,
})
.promise();
return { passThrough, promise };
};
export const handler = async (event: CopyFileEvent): Promise<string> => {
const responseStream = await downloadFile(event.fileUrl);
const { passThrough, promise } = uploadFromStream(responseStream, event.fileName, 'test-bucket');
responseStream.data.pipe(passThrough);
return promise
.then((result) => {
return result.Location;
})
.catch((e) => {
throw e;
});
};
Credit to: https://dev.to/vikasgarghb/streaming-files-to-s3-using-axios-h32