1

Basically I'm using next.js and I'm making a program that generates images using Openai's Dalle2 API (artificial intelligence), and then uploads it onto my firebase storage. How do I download these images when they are generated and upload them to the firebase storage? Everytime i try, I just end up getting a 'application/octet-stream' file type uploaded and it doesn't work.

This is my code:

    const [selectedFile, setSelectedFile] = useState(null);

    const generateImage = async () => {
        console.log('openai image start')
        const prompt = captionRef.current.value

        try {
            const response = await openai.createImage({
                prompt: prompt,
                n: 1,
                size: "256x256",
            })

            const imageUrl = response.data.data[0].url
            console.log('openai image generated')
            setSelectedFile(imageUrl)
            console.log(selectedFile)

        } catch (error) {
            if (error.response) {
                console.log(error.response.status);
                console.log(error.response.data);
            } else {
                console.log(error.message);
            }
        }
    }

    const uploadPost = async () => {
        if (loading) return;
        setLoading(true);
        //create post and add to firestore posts collection
        //get post id for new post
        //upload image to firebase storage with post id
        //get a download url from firebase storage and update original post with image 

        const docRef = await addDoc(collection(db, 'posts'), {
            username: session.user.username,
            caption: captionRef.current.value,
            profileImg: session.user.image,
            timestamp: serverTimestamp()
        })

        console.log("New doc added with ID", docRef.id);
        console.log(selectedFile)

        //**PROBLEM**
        const imageRef = ref(storage, `posts/${docRef.id}/image`)
        await uploadBytes(imageRef, selectedFile).then(async snapshot => {
            const downloadURL = await getDownloadURL(imageRef)
            await updateDoc(doc(db, 'posts', docRef.id), {
                image: downloadURL
            })
        })

        setLoading(false)
        setSelectedFile(null)
    }
EternalObserver
  • 517
  • 7
  • 19
adooze
  • 11
  • 3
  • You can't pass the resource URI directly to `uploadBytes`. As per the docs: "If a Blob, File, or Uint8Array isn't available, you can use the uploadString() method to upload a raw, base64, base64url, or data_url encoded string to Cloud Storage.". Link to docs: https://firebase.google.com/docs/storage/web/upload-files#web-version-9_3 Let me know if this works! – EternalObserver Jan 06 '23 at 04:28
  • I tried that as well, doesn't work. When I use the data_url method, it says my string is not formatted for that. the URL: https://oaidalleapiprodscus.blob.core.windows.net/private/org-kXw0nycp2t8g8dBHsU2sy9BU/user-h9rCOZeQBt8IvWwbQyZRFCp8/img-gmZ9EkIlAKmeUKI3Pm7f6lJc.png?st=2023-01-06T03%3A32%3A20Z&se=2023-01-06T05%3A32%3A20Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-06T03%3A33%3A59Z&ske=2023-01-07T03%3A33%3A59Z&sks=b&skv=2021-08-06&sig=4aYBAV%2B107BAdRUiEpAWFndF%2B/mHw063LKZGNI9zabA%3D – adooze Jan 06 '23 at 04:41
  • did you convert the URL in the base64 format as specified? – EternalObserver Jan 06 '23 at 04:53
  • not sure how to do that – adooze Jan 06 '23 at 05:30
  • Check this post, the accepted answer should work: https://stackoverflow.com/questions/22172604/convert-image-from-url-to-base64 – EternalObserver Jan 06 '23 at 05:41

0 Answers0