Back

'Invalid document structure: Attribute "chunksTotal" has invalid type'

  • 0
  • Self Hosted
  • Storage
  • REST API
Pwncake
11 Oct, 2024, 12:14

Hello, I am trying to upload a file to the Appwrite storage using the REST API. But I always get following error:

TypeScript
Error uploading file chunk: {
  message: 'Invalid document structure: Attribute "chunksTotal" has invalid type. Value must be a valid range between 0 and 2,147,483,647',
  code: 400,
  type: 'document_invalid_structure',
  version: '1.6.0'
}

Isn't the chunksTotal attribute set by Appwrite? I can't find anything about setting chunksTotal in the docs. I tried to append it to the formData but it also didn't help.

I am using following code:

TypeScript
  const filePath = "./example.png";
  const CHUNK_SIZE = 5 * 1024 * 1024; // 5MB
  const fileStats = await fs.stat(filePath);
  const fileSize = fileStats.size;
  const fileName = path.basename(filePath)

  let start = 0;
  let end = Math.min(CHUNK_SIZE, fileSize - 1);
  let fileId = null;

  while (start < fileSize) {
    const chunk = createReadStream(filePath, { start, end });

    const formData = new FormData();
    if (fileId) {
      formData.append("fileId", fileId);
    } else {
      formData.append("fileId", "unique()");
    }
    formData.append("file", chunk, fileName);
    formData.append("permissions[]", 'read("any")');

    const headers = {
      ...formData.getHeaders(),
      "Content-Range": `bytes ${start}-${end}/${fileSize}`,
      "X-Appwrite-Project": "<project ID>",
      "X-Appwrite-ID": fileId,
    };
    const response = await axios.post(
      `<api endpoint>/v1/storage/buckets/<bucket id>/files/`,
      formData,
      {
        headers: headers
      }
    );

    if (!fileId) {
      fileId = response.data.$id;
    }
    start = end + 1;
    end = Math.min(start + CHUNK_SIZE - 1, fileSize - 1);
  }

  console.log("File upload completed!");
TL;DR
Error message 'Invalid document structure: Attribute "chunksTotal" has invalid type. Value must be a valid range between 0 and 2,147,483,647' when trying to upload file to Appwrite storage using REST API. Issue stems from setting 'chunksTotal' attribute incorrectly. Ensure 'chunksTotal' value fits within specified range. Solution: Correct 'chunksTotal' configuration.
faye
11 Oct, 2024, 12:39

May I ask why not use the SDK?

Pwncake
11 Oct, 2024, 12:45
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