Question

Error use spaces with @aws-sdk/lib-storage

The code:

import { Upload } from "@aws-sdk/lib-storage";
import { s3Client } from "./s3-client";

const bucket = "xxxx";

export async function uploadFile(name: string, content: string) {
  const parallelUploads3 = new Upload({
    client: s3Client,
    params: {
      Bucket: bucket,
      Key: name,
      Body: content,
    },
    leavePartsOnError: true,
  });
  await parallelUploads3.done();
}

And I’ve set the CORS rule for this bucket as:

  CORSRules: [
    {
      AllowedHeaders: [ "*" ],
      AllowedMethods: [ "GET", "PUT", "DELETE", "HEAD", "POST" ],
      AllowedOrigins: [ "*" ],
      ExposeHeaders: [ "ETag", "x-amz-*" ]
    }
  ]

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.

Bobby Iliev
Site Moderator
Site Moderator badge
August 21, 2023

Hi there,

Would you mind sharing the exact error that you are getting?

One thing that stands out from the information that you’ve provided so far is that the s3Client configuration might not be correct. Here’s an example of how that might look:

import { S3Client } from "@aws-sdk/client-s3";

export const s3Client = new S3Client({
  region: "nyc3", // Your DigitalOcean Space's region
  credentials: {
    accessKeyId: "YOUR_ACCESS_KEY",
    secretAccessKey: "YOUR_SECRET_KEY",
  },
  endpoint: "https://nyc3.digitaloceanspaces.com", // Your DigitalOcean Space's endpoint
});

I could also suggest trying a standard S3 upload to verify if it is working using PutObject or similar. Here is a quick example:

import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { Readable } from 'stream';

// Set up the client configuration for DigitalOcean Spaces
const s3Client = new S3Client({
  region: "nyc3", // Your DigitalOcean Space's region
  credentials: {
    accessKeyId: "YOUR_ACCESS_KEY",
    secretAccessKey: "YOUR_SECRET_KEY",
  },
  endpoint: "https://nyc3.digitaloceanspaces.com", // Your DigitalOcean Space's endpoint
});

const bucket = "xxxx"; // Your bucket name

export async function uploadFile(name: string, content: string) {
  // You may need to convert content to a Buffer or Readable stream if it's not already
  const body = Readable.from(content);

  // Create the PutObjectCommand with required parameters
  const command = new PutObjectCommand({
    Bucket: bucket,
    Key: name,
    Body: body,
    ContentType: "application/octet-stream", // Or any appropriate content type for your file
  });

  // Execute the command and handle the response
  try {
    const response = await s3Client.send(command);
    console.log(`Successfully uploaded file. ETag: ${response.ETag}`);
  } catch (error) {
    console.error(`Error uploading file: ${error}`);
  }
}

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel