#how can i upload a image to storage using a url

1 messages · Page 1 of 1 (latest)

hazy rapids
#

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?

warped cairn
# lofty herald You cant

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

lofty herald
warped cairn
# lofty herald You should be able to upload via a buffer

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;
  }
}
#

Seems correct to me

#
Error uploading image: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type number (3798243238)
#

Ah nvm see the error imageName should be an string

warped cairn
#

Appwrite

#

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

lofty herald
#

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

Btw there's a new version of the node sdk

warped cairn
lofty herald
warped cairn
#
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 imageBlob = await imageResponse.blob();
    const imageFile = sdk.InputFile.fromBlob(imageBlob, imageName);

    const result = await storage.createFile(bucketId, ID.unique(), imageFile);
    console.log(result);
    return result;
  } catch (error) {
    console.error("Error uploading image:", error);
    throw error;
  }
}

Even if I use that fromBlob it doesnt work... It seems like InputFile in this newer version is not defined