Back

how can i upload a image to storage using a url

  • 0
  • Storage
ianmont
20 Oct, 2023, 02:53

how can i achive uploading a file to storage, using a url?

i attach a photo of an example i want to do, is that possible and any hints on how to do it?

TL;DR
Developers are struggling to upload an image to storage using a URL, facing a TypeError related to the path argument. The solution involves using `InputFile.fromBuffer` instead of `InputFile.fromBlob`. This allows the developers to fetch the image, create a buffer, and then utilize the buffer to upload the image successfully.
Drake
20 Oct, 2023, 03:45

You cant

Hexi
31 May, 2024, 23:19

There is no possible way??? Tried to fetch it and parse the buffer to it but did not work sadly

Steven
31 May, 2024, 23:20

You should be able to upload via a buffer

Hexi
31 May, 2024, 23:22
TypeScript

async function uploadImage(
  imageUrl,
  imageName,
  bucketId = process.env.IMAGES_BUCKET_ID
) {
  try {
    const imageResponse = await fetch(imageUrl);
    if (!imageResponse.ok) {
      throw new Error("Failed to fetch image from URL");
    }

    const arrayBuffer = await imageResponse.arrayBuffer();
    const buffer = Buffer.from(arrayBuffer);

    const inputFile = sdk.InputFile.fromBuffer(buffer, imageName);

    const result = await storage.createFile(bucketId, ID.unique(), inputFile);
    return result;
  } catch (error) {
    console.error("Error uploading image:", error);
    throw error;
  }
}
Hexi
31 May, 2024, 23:23

Seems correct to me

Hexi
31 May, 2024, 23:24
TypeScript
Error uploading image: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type number (3798243238)
Hexi
31 May, 2024, 23:26

Ah nvm see the error imageName should be an string

Steven
31 May, 2024, 23:29

What sdk are you using?

Hexi
31 May, 2024, 23:29

Appwrite

Hexi
31 May, 2024, 23:29

const sdk = require("node-appwrite");

Steven
1 Jun, 2024, 00:25

Maybe you can use InputFile.fromBlob(await imageResponse.blob())

Btw there's a new version of the node sdk

Hexi
1 Jun, 2024, 08:06

I tried updating lately and had loads of errors... So I just went back to the old one

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more