Image/Audio file Uploaded to S3 bucket from Android phone (using react-native) is of type application/octet-stream

0

I'm uploading audio/image files from my Android phone using react native. I'm using aws-sdk to upload and upload works fine without any problem and I could see those uploaded files on my S3 bucket.

The problem is that, when I download the files that uploaded, it is corrupted or it is in different format.

I've made sure that I'm using the correct ContentType (audio/wav, image/jpeg). However, after downloading the file from S3 bucket, I see the file is of type application/octet-stream as shown below.

What am I missing here? Appreciate any help. Thank you.

content type of downloaded file from S3 bucket

import RNFS from 'react-native-fs';
import AWS from 'aws-sdk';
import base64 from 'react-native-base64'

uploadFileToS3 = async (file, fileInfo, details) => {

    const BUCKET_NAME = my_bucket
    const REGION = my_region
    const IAM_USER_KEY = my_key
    const IAM_USER_SECRET = my_secret

    const s3bucket = new AWS.S3({
        accessKeyId: IAM_USER_KEY,
        secretAccessKey: IAM_USER_SECRET,
        region: REGION,
        signatureVersion: 'v4',
    });

    const contentType = fileInfo.mimeType;
    const contentDeposition = `inline;filename="${fileInfo.name}"`;
    const fPath = file;
    const base64_data = await RNFS.readFile(fPath, 'base64');
    const arrayBuffer = base64.decode(base64_data);

    return new Promise((resolve, reject) => {
        s3bucket.createBucket(() => {
            const params = {
                Bucket: BUCKET_NAME,
                Key: `my_folder/${fileInfo.name}`,                     
                Body: arrayBuffer,
                ContentDisposition: contentDeposition,
                ContentType: contentType,
            };

            s3bucket.upload(params, (error, data) => {
                if (error) {
                    reject(error);
                } else {
                    resolve(data);
                }
            });
        });
    });
};
asked 10 months ago732 views
2 Answers
0
Accepted Answer

Thanks @Riku for your inputs.

I fixed the problem by replacing

base64.decode(base64_data);

with

AWS.util.base64.decode(base64_data)

I'm yet to investigate further on the difference bewteen AWS base64 decode and base64.decode() from react-native-base64 package.

Probably S3 likes the file to be decoded from aws-sdk package.. ;-) :D

Thanks again and have a nice day.

answered 10 months ago
profile picture
EXPERT
reviewed 10 months ago
0

How about making the ContentType be automatically retrieved from the file as follows?

ContentType: mimetypes.get(file)
profile picture
EXPERT
answered 10 months ago
profile picture
EXPERT
reviewed 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions