TypeError:在Expo react native中使用AWS Amplify存储时,未定义不是对象(评估“_storage.default. put”)

ddrv8njm  于 2023-02-05  发布在  React
关注(0)|答案(1)|浏览(109)

我希望在我的Expo react原生项目中添加上传图像和视频到S3的功能。我使用ImagePicker来允许用户选择图像和视频......这很好用。我在使用签名URL上传视频时遇到了问题-类型为空,因此无法播放-出于某种原因,这似乎对图像没有影响,所以我在AWS Amplify中使用了this tutorial,因为它似乎提供了一种更健壮的方式将内容推送到S3 bucket。我根据文档配置了Amplify-它产生了一个S3 bucket。然后,我按照文档中的说明设置auth并运行

amplify add storage

为了给我的项目增加存储空间,我加入了一个bucket来放大设置,然后我给ImagePicker函数添加了以下代码:

const imageName = result.assets[0].uri.replace(/^.*[\\\/]/, '');
        const fileType = mime.lookup(result.assets[0].uri);
        const access = { level: "public", contentType: {fileType} };
        const imageData = await fetch(result.assets[0].uri)
        const blobData = await imageData.blob()
        console.log("Image name and filetype " + imageName + " and " + fileType);
        try {
          await Storage.put(imageName, blobData, access)
        } catch (err) {
          console.log('error: ', err)
        }

结果是:

Image name and filetype E5E29BF9-6CD2-4D87-8B63-7FA9B0BE4A80.mov and video/quicktime
    error:  [TypeError: undefined is not an object (evaluating '_storage.default.put')]

我不确定如何进行故障排除以及为什么会出现该错误。如有任何帮助,我们将不胜感激

nmpmafwu

nmpmafwu1#

问题最终是我从错误的库导入存储:

import Storage from '@aws-amplify/storage'
import Amplify from "@aws-amplify/core";

需要:

import { Amplify, Storage } from 'aws-amplify';

我在一个教程中找到了前者,所以它把我引上了歧途。

相关问题