bucket.upload(params).promise()

bucket.upload(params).promise()

By medeb | personalblog | 16 May 2025


1. bucket.upload(params).promise()

Hello! I understand you're having trouble updating some old Amazon S3 code that uses bucket.upload(params).promise(). This method is deprecated, and it's recommended to use the new APIs based on await client.send(new UploadCommand(params)).

You can update your code using the new APIs, which are detailed in the AWS SDK.

2. Installing the New AWS SDK v3 Packages

Make sure you have installed the necessary packages for the AWS SDK v3. If you don't have them, run the following commands in your terminal:

Bash

npm install @aws-sdk/client-s3 @aws-sdk/lib-storage
3. Updating the uploadToS3 Function

Here's how you can update your uploadToS3 function to use UploadCommand:

JavaScript

import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";

async function uploadToS3(bucketName, params) {
const client = new S3Client({ region: "YOUR_REGION" }); // Replace YOUR_REGION with your AWS region

try {
const upload = new Upload({
client: client,
params: {
Bucket: bucketName,
Key: params.Key,
Body: params.Body,
ContentType: params.ContentType, // If you have a ContentType
// ... other parameters if needed (ACL, Metadata, etc.)
},
});

const data = await upload.done();

return {
Location: `https://${bucketName}.s3.${client.config.region}.amazonaws.com/${params.Key}`,
// You can add other data information if needed
};
} catch (err) {
console.error("Error uploading to S3:", err);
throw err; // Propagate the error for handling in your calling code
}
}
Explanations:

S3Client: Creates an S3 client with your AWS region.
Upload: Uses the Upload class from @aws-sdk/lib-storage to handle uploads, especially large file uploads.
params: Parameters include Bucket, Key, Body, and optionally ContentType and other options.
upload.done(): Waits for the upload to complete.
Location: Constructs the public URL of the uploaded object.
Error Handling: The try...catch block handles potential errors during the upload.

c2e114ac63b4fec6b40095d6b5e1a611fd8617176284f0033873e3ff0fb3c55c.png

How do you rate this article?

1



personalblog
personalblog

My daily experience in crypto world

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.