Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native missing readAsArrayBuffer

I'm trying to convert an uri image (retrieved with react-native-image-picker) to an array buffer to upload the file to s3 (since s3 does not support form-data for put requests I can't use it)

this is my code

    let fetched = await fetch(pictureInfo.uri)
    console.log("Fetched uri:", fetched)
    let arrayBuffer = await fetched.arrayBuffer()

but running it I receive

Error: FileReader.readAsArrayBuffer is not implemented

How can I convert it to a buffer and upload to s3 without converting it to base64?

like image 456
nicecatch Avatar asked Apr 05 '18 19:04

nicecatch


1 Answers

Without going for the base64 method, you can upload the image file directly using the react-native-fetch-blob package.

Assuming that you have aws-sdk for react-native

// Add Config

AWS.config.update({
    region: // Your Region,
    accessKeyId: // Your Access key,
    secretAccessKey: // Your Secret Key,
    sessionToken: // Your Session Token,
});

// Initialize
const s3 = new AWS.S3();

// Get Signed Url
const signedUrl = await s3.getSignedUrl('putObject', {
    Bucket: // Your Bucket,
    Key: // Your Key,
    ContentType: // Content Type, example `image/jpg`,
});


const s3 = new AWS.S3();

const upload = await RNFetchBlob.fetch('PUT', signedUrl, {
    'Content-Type': // Content Type
}, RNFetchBlob.wrap(localPath)); // where local path would be your local image path ${image.uri}
like image 65
Pritish Vaidya Avatar answered Nov 15 '22 03:11

Pritish Vaidya