通过AWS React Native Image Upload使用存储类进行放大 [英] React Native Image Upload via aws Amplify using Storage class
问题描述
我有一张图片.我想使用aws-amplify将其上传到S3.所有的Storage类上载示例都使用文本文档.但是,我想上传一张图片.我正在使用expo,它没有react-native-fetch-blob的支持,而react native没有blob的支持....
I have an image. I want to upload it to S3 using aws-amplify. All the Storage class upload examples are using text documents; however, I would like to upload an image. I am using expo which does not have support from react-native-fetch-blob, and react native does not have blob support... yet.
所以我的选择似乎是:
- 通过lambda创建节点服务.
- 仅将base64信息上传到S3,而不是Blob.
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status === 'granted') {
const image = await ImagePicker.launchImageLibraryAsync({
quality: 0.5,
base64: true
});
const { base64 } = image;
Storage.put(`${username}-profileImage.jpeg`, base64);
}
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status === 'granted') {
const image = await ImagePicker.launchImageLibraryAsync({
quality: 0.5,
base64: true
});
const { base64 } = image;
Storage.put(`${username}-profileImage.jpeg`, base64);
}
这正确吗?
推荐答案
使用RN BLOB支持版本编辑更新的答案:现在,我已经能够解决此问题,因为React Native宣布了对blob的支持,并且现在我们只需要uri.请参见以下示例:
EDIT FOR UPDATED ANSWER WITH RN BLOB SUPPORT VERSION: I have been able to solve this now that React Native has announced blob support and now we only need the uri. See the following example:
uploadImage = async uri => {
const response = await fetch(uri);
const blob = await response.blob();
const fileName = 'profileImage.jpeg';
await Storage.put(fileName, blob, {
contentType: 'image/jpeg',
level: 'private'
}).then(data => console.log(data))
.catch(err => console.log(err))
}
旧答案
对于您而言,这一切都是如此.我最终使用了 https://github.com/benjreinhart/react-native-aws3 这完美地工作了!但不是首选的解决方案,因为我想使用aws-amplify.
For all you get to this. I ended up using https://github.com/benjreinhart/react-native-aws3 This worked perfectly! But not the preferred solution as I would like to use aws-amplify.
这篇关于通过AWS React Native Image Upload使用存储类进行放大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!