How to upload images to Digital Ocean Spaces?

I am using GraphQL for creating APIs

type Mutation {
    uploadImage(image: Upload!): Image
}

I am using Upload scalar.

In the frontend, getting the file from the input field.

const filePickerPath = async e => {
    e.preventDefault()
    const file = e.target.files[0]

    uploadImageMutation({
      variables: {
        image: file
      }
    }).then(response => {
      console.log(response);
    }).catch(error => {
      console.log(error);
    });

Calling the uploadImage mutation by passing the File constructor as a variable.

Inside the uploadImage resolver, after getting the data input (argument).

const data = { ...input.image.file }

console.log("Spaces start");
const params = {
  Bucket: process.env.DO_SPACES_BUCKET,
  Key: path,
  Body: data,
  ACL: "public-read"
};

spaces.putObject(params, (error, data) => {
  if (error) {
    console.error("Error: uploadImage");
    console.log(error, error.stack);
  }
  else {
    console.log("data");
    console.log(data);
  }
});
console.log("Spaces end");

After that I get this error

Expected params.Body to be a string, Buffer, Stream, Blob, or typed array object

I even tried convert the data input as a Buffer but that didn't work either.

No Comments Yet