I’m trying to upload a PDF picked in React Native to Amazon s3. In order to do so, I sent the PDF encoded in base64 to my nodejs/adonis server and transformed it into a buffer (because I don’t wanna save it in base 64).
The PDF is actually being saved in my S3 Bucket, but not properly, as I can neither open it nor download it.
That’s how I’m trying to do it:
public async storeFile({ request }: HttpContextContract) {
const hash = crypto.randomBytes(32).toString('hex');
let key = `PDF-${hash}`;
//create a single key for each file
const file = request.input('file');
const fileBuffer = Buffer.from(file, 'base64');
//transform the file encoded in base64 into a buffer
await S3Service.uploadManually2(fileBuffer, key);
}
public async uploadManually2(file: Buffer, key: string): Promise<string> {
const params = {
Bucket: Env.get('AWS_BUCKET_NAME'),
Body: file,
Key: key,
ContentType: 'application/pdf',
ACL: 'public-read',
}
await s3.send(new PutObjectCommand(params))
const url = `https://${Env.get('AWS_BUCKET_NAME')}.s3.amazonaws.com/${key}`
return url
}
2
Answers
My goal in the first place was to send a PDF from react-native to S3. Firstly I tried to send the file to the server and then to s3, but the way it worked for me was to send directly to s3 from react-native. To do so, I installed the library "react-native-aws3" and used the function "put".
That's the code I used:
The simplest fix here would be to convert the buffer form of the PDF file to a readable stream and then pass that stream to S3.