反应原生缺少readAsArrayBuffer

nic*_*tch 11 blob amazon-s3 filereader react-native

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

这是我的代码

    let fetched = await fetch(pictureInfo.uri)
    console.log("Fetched uri:", fetched)
    let arrayBuffer = await fetched.arrayBuffer()
Run Code Online (Sandbox Code Playgroud)

但我收到它

错误:未实现FileReader.readAsArrayBuffer

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

Pri*_*dya 1

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

假设你aws-sdkreact-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}
Run Code Online (Sandbox Code Playgroud)