我需要将图像直接上传到 S3 存储桶。我正在使用 react native 和 react-native-image-picker 来选择照片。这将返回一个本地图像 uri。这是我现在的代码。
ImagePicker.showImagePicker(options, response => {
var bodyFormData = new FormData(); // If I don't use FormData I end up
// uploading the json not an image
bodyFormData.append('image', {
uri: response.uri, // uri rather than data to avoid loading into memory
type: 'image/jpeg'
});
const uploadImageRequest = {
method: 'PUT',
url: presignedS3Url,
body: bodyFormData,
headers: {
'Content-Type: 'multipart/form-data'
}
};
axios(uploadImageRequest);
});
这几乎可以工作..当我检查我的 S3 存储桶时,我有一个几乎是图像的文件。它具有以下格式
--Y_kogEdJ16jhDUS9qhn.KjyYACKZGEw0gO-8vPw3BcdOMIrqVtmXsdJOLPl6nKFDJmLpvj^M
content-disposition: form-data; name="image"^M
content-type: image/jpeg^M
^M
<Image data>
如果我手动进入并删除标题,那么我就有了我的形象!但是,我需要将图像直接上传到 S3,客户将抓取并期望已经采用正确的图像格式。
我可以使用 response.data 并解码为字符串并直接上传它来完成这项工作,但为了记忆,我宁愿不这样做。