Report this

What is the reason for this report?

I keep getting "Cannot read properties of undefined (reading 'byteLength')" error when uploading files to a space using aws-sdk for node.js

Posted on March 15, 2022

Hello to all, I’m completely new to Digital Ocean and to cloud storage in general, but even following step by step this tutorial I get the error mentioned in the title. I think that it’s is related to the fact that I’m not authenticated correctly, but I really don’t know why.

The important bits of code are this:

import fs from "fs";
import { S3, PutObjectCommand } from "@aws-sdk/client-s3";

const s3Client = new S3({
  endpoint: "https://fra1.digitaloceanspaces.com",
  region: "fra1",
  credentials: {
    accessKeyId: process.env.SPACES_KEY,
    secretAccessKey: process.env.SPACES_SECRET,
  },
});

and this:

const fileContent = await new Promise((resolve) =>
      fs.readFile("output.webp", (err, data) => resolve(data))
    );
    try {
      const data = await s3Client.send(
        new PutObjectCommand({
          Bucket: "name-of-bucket",
          Key: "test.webp",
          Body: fileContent,
        })
      );
      console.log(
        "Successfully uploaded object: " +
          bucketParams.Bucket +
          "/" +
          bucketParams.Key
      );
      return data;
    } catch (err) {
      console.log("Error", err);
    }

I followed the steps to generate the API keys to access the Space and then I stored them in a .env file, as per this. But I’m not setting anything else, maybe I should, I really don’t know. The SDK version is 3.54.0. Any help would be much appreciated

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.