4

我想将照片上传到 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)));

获得图像后,我通过以下方式将图像上传到 s3 存储桶{ Storage } from 'aws-amplify'

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

4

1 回答 1

4

只要弄清楚问题是什么。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));
    });
});
于 2018-06-19T07:53:11.490 回答