Amazon web services 通过Expo ImagePicker将本机图像上传至aws S3
我想上传一张照片到S3存储桶,我目前正在使用Expo和aws放大来实现这一点 获取本地映像的代码如下所示:Amazon web services 通过Expo ImagePicker将本机图像上传至aws S3,amazon-web-services,react-native,amazon-s3,expo,aws-amplify,Amazon Web Services,React Native,Amazon S3,Expo,Aws Amplify,我想上传一张照片到S3存储桶,我目前正在使用Expo和aws放大来实现这一点 获取本地映像的代码如下所示: import { ImagePicker } from 'expo'; ImagePicker.launchImageLibraryAsync({ mediaTypes: 'Images', allowsEditing: true, aspect: [1, 1], base64: true }) .then(image => dispatch(pickImageSu
import { ImagePicker } from 'expo';
ImagePicker.launchImageLibraryAsync({
mediaTypes: 'Images',
allowsEditing: true,
aspect: [1, 1],
base64: true
})
.then(image => dispatch(pickImageSuccess(image)))
.catch(err => dispatch(piclImageFail(err)));
在获得图像后,我通过“aws amplify”中的{Storage}将图像上传到s3 bucket中
import { Storage } from 'aws-amplify';
const file = image.base64;
const imageName = image.uri.replace(/^.*[\\\/]/, '');
console.log(imageName);
const access = { level: "public", contentType: 'image/jepg' };
Storage.put(`${username}/${imageName}`, file, access)
.then(succ => console.log(succ))
.catch(err => console.log(err));
之后
我上传了图像,我试图在S3存储桶中打开图像。图像无法正常打开,除非我在S3存储桶中将文件标记为public,否则我无法公开访问图像。请找出问题所在。存储器中的put()方法要求参数使用blob。以下是我为实现这一目标所做的工作
import { ImagePicker } from 'expo';
import mime from 'mime-types';
import { Storage } from 'aws-amplify';
const imageName = image.uri.replace(/^.*[\\\/]/, '');
const fileType = mime.lookup(image.uri);
const access = { level: "public", contentType: fileType, };
fetch(image.uri).then(response => {
response.blob()
.then(blob => {
Storage.put(`${username}/${imageName}`, blob, access)
.then(succ => console.log(succ))
.catch(err => console.log(err));
});
});
可能是因为输入错误:
contentType:'image/jepg'
,应该是image/jpeg
@Cherniv谢谢,我注意到,购买它仍然不起作用。包“mime-types”无法使用,因为它导入了节点标准库模块“path”。你让它起作用了吗?