【问题标题】:React-native upload image to aws S3 via Expo ImagePickerReact-native 通过 Expo ImagePicker 将图像上传到 aws S3
【发布时间】:2018-11-28 00:42:21
【问题描述】:

我想将照片上传到 S3 存储桶,我目前使用 Expo 和 aws-amplify 来解决这个问题。

获取本地图片的代码如下:

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)));

获取图片后,我通过{ Storage } from 'aws-amplify'将图片上传到s3存储桶

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 存储桶中将文件标记为公开。

【问题讨论】:

  • 可能是因为拼写错误:contentType: 'image/jepg',应该是image/jpeg
  • @Cherniv 谢谢,我注意到了,买还是不行。

标签: amazon-web-services react-native amazon-s3 expo aws-amplify


【解决方案1】:

只要找出问题所在。 Storage 中的 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));
    });
});

【讨论】:

  • 包“mime-types”不能使用,因为它导入了Node标准库模块“path”。
猜你喜欢
  • 2021-06-07
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 2020-03-02
  • 2020-04-23
  • 2020-06-30
  • 1970-01-01
  • 2019-07-25
相关资源
最近更新 更多