Question

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

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


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hello,

As far as I can see, you’ve set the region to fra1, however, in the docs it is suggested the following:

Due to an AWS-specific behavior in all versions of the SDK except Python 3, to successfully create a new Space, you must specify an AWS region, such as us-east-1, in your configuration. This is because, when creating a Space, the SDK sends an entirely different payload if a custom region is specified, which results in an error. Region does not affect any other examples listed in this page.

Note, specifying us-east-1 does not result in slower performance, regardless of your Space’s location. The SDK checks the region for verification purposes but never sends the payload there. Instead, it sends the payload to the specified custom endpoint.

Note that you should keep your endpoint as it is.

I could suggest trying this out. Let me know how it goes!

Best,

Bobby