反应原生缺少readAsArrayBuffer

时间:2018-04-05 19:53:50

标签: react-native amazon-s3 blob filereader

我正在尝试将uri图像(使用react-native-image-picker检索)转换为数组缓冲区,以将文件上传到s3(因为s3不支持put请求的表单数据,我无法使用它)

这是我的代码

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

但运行它我收到了

  

错误:未实施FileReader.readAsArrayBuffer

如何将其转换为缓冲区并上传到s3而不将其转换为base64?

1 个答案:

答案 0 :(得分:0)

如果不使用base64方法,您可以使用react-native-fetch-blob包直接上传图像文件。

假设您aws-sdk

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}