Home > Back-end >  TypeError: undefined is not an object (evaluating '_storage.default.put') while using AWS
TypeError: undefined is not an object (evaluating '_storage.default.put') while using AWS

Time:02-02

I'm looking to add the ability to upload images and videos to S3 in my Expo react native project. I'm using ImagePicker to allow the user to pick images and videos...that works fine. I was having issues with video uploaded using signed URLs - the type was left empty so playback wasn't possible - for some reason, it didn't seem to matter for images. So I followed this tutorial for AWS Amplify since it seemed to provide a more robust way of pushing content to S3 buckets. I configured Amplify according to the documentation - it resulted in an S3 bucket. I then setup auth as described in the documentation and ran

    amplify add storage

To add storage to my project - I included the bucket that amplify setup for me. I then added the following code to my ImagePicker function:

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

Which results in:

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

I'm not sure how to proceed in troubleshooting this and why I'm getting that error. Any help would be appreciated

CodePudding user response:

The problem ended up being that I was importing Storage from the wrong library:

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

Needed to be:

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

I found the former in a tutorial so it led me down the wrong path.

  • Related