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);
                }
            });
        });
    });
};
posta un anno fa766 visualizzazioni
2 Risposte
0
Risposta accettata

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.

con risposta un anno fa
profile picture
ESPERTO
verificato un anno fa
0

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

ContentType: mimetypes.get(file)
profile picture
ESPERTO
con risposta un anno fa
profile picture
ESPERTO
verificato un anno fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande