【问题标题】:TypeError: undefined is not an object (evaluating '_storage.default.put') while using AWS Amplify Storage in Expo react nativeTypeError: undefined is not an object (evaluating \'_storage.default.put\') while using AWS Amplify Storage in Expo 反应本机
【发布时间】:2023-02-02 07:10:24
【问题描述】:

我希望在我的 Expo React Native 项目中添加将图像和视频上传到 S3 的功能。我正在使用 ImagePicker 来允许用户选择图像和视频……效果很好。我在使用签名 URL 上传视频时遇到问题 - 类型留空,因此无法播放 - 出于某种原因,它似乎对图像无关紧要。因此,我关注this tutorial 的 AWS Amplify,因为它似乎提供了一种将内容推送到 S3 存储桶的更强大的方式。 我根据文档配置了 Amplify - 它生成了一个 S3 存储桶。然后我按照文档中的描述设置身份验证并运行

    amplify add storage

为我的项目添加存储 - 我包括了为我放大设置的存储桶。然后我将以下代码添加到我的 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')]

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

【问题讨论】:

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


    【解决方案1】:

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

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

    需要是:

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

    我在教程中找到了前者,所以它让我走上了错误的道路。

    【讨论】:

      猜你喜欢
      • 2022-12-21
      • 2023-02-18
      • 2022-12-03
      • 2019-11-02
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      相关资源
      最近更新 更多