A step-by-step guide on uploading a file to an AWS S3 bucket using a pre-signed URL in a Node

I am currently using S3 upload function in Node.js to upload files to an S3 bucket. The frontend of the application is built on Angular. However, my client now requires that all uploads be directed to the S3 bucket via a presigned URL. I am wondering if this requirement is due to security concerns. Below is the code snippet that I am using for uploading files to the S3 Bucket:

async function uploadFile(object){
//object param contains two properties 'image_data' and 'path'
  return new Promise(async(resolve, reject) => {
        var obj = object.image_data;
        var imageRemoteName = object.path+'/'+Date.now()+obj.name;
        AWS.config.update({
          accessKeyId: ACCESS_KEY,
          secretAccessKey: SECRET_KEY,
          region: REGION
        })

        var s3 = new AWS.S3()
        s3.upload({
          Bucket: BUCKET,
          Body: obj.data,
          Key: imageRemoteName
        })
        .promise()
        .then(response => {
            console.log(`done! - `, response)
            resolve(response.Location)
        })
        .catch(err => {
            console.log('failed:', err)
        })

  })
}

Any assistance or insight on this matter would be highly appreciated. Thank you!

Answer №1

When it comes to security, the method you choose between calling upload or generating a pre-signed URL doesn't really matter as long as your Angular application is not running this code on the client side. If it is, then every user of your application could potentially gain access to your AWS credentials. However, using a server like express to run this code would provide a more secure environment.

AWS offers guidance on how to upload objects using a pre-signed URL. The general steps involve importing necessary libraries, creating an S3 client with access credentials, and executing a PutObjectCommand. This will allow you to upload an object and obtain a signed URL for downloading, if required.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to style HTML content with MathJax following its retrieval with jQuery.load?

I recently encountered an issue while using jQuery.load to load a new page. The content on the original page is being treated strangely in some way. Specifically, I have code on the original page that formats LaTeX commands with MathJax: <script type=" ...

Leveraging NodeJs Environment Variables within ViteJS React Components

When using ViteJs to build a ReactJs Project, the vite.config.ts file contains the following: const isDev = process.env["DFX_NETWORK"] !== "ic" const network = process.env.DFX_NETWORK || (process.env.NODE_ENV === "production&quo ...

Require checkboxes in AngularJS forms

Currently, I have a form that requires users to provide answers by selecting checkboxes. There are multiple checkboxes available, and AngularJS is being utilized for validation purposes. One essential validation rule is to ensure that all required fields a ...

Error: The term "User" has not been previously defined

I encountered an issue while attempting to authenticate via vkontakte (vk.com) using passport-vkontakte. Error: A ReferenceError: User is not defined Below is the content of my auth.js file. var express = require('express'); var passport ...

jQuery will envelop the HTML elements in an inconsequential div

Imagine a website that is visually complex, with various styles and images positioned in different ways. What if we wanted to add a small overlay icon above each image? At first, the solution might seem simple - just use absolute positioning for a span el ...

Provide the aggregated content within d3's text() or html() function

Below is my d3 code snippet: grouping.append('foreignObject').html(function (d) { var string = '<p>hello, {{ "there" }} <some-directive></some-directive></p>'; string = $compile(string)(scope); return stri ...

Ways to automatically close the external window upon logging out in Angular 12

I have successfully created an external window in my Angular application. Everything is working as expected, but I am facing an issue when trying to automatically close the external window upon user logout. Although I have written the code below and it wo ...

Ways to modify an object item using a loop

I am facing an issue while trying to add a property to an object in my code. I have a list of customers and for each customer, I need to add an array containing the emails that were sent to them. However, I am unable to successfully add this array to the ...

How can I efficiently transfer a JavaScript array to a PHP script using the GET method?

My web application is designed with jQuery allowing users to manipulate visual elements. The next step is sending the JavaScript object state to PHP for storage in a database. While I prefer using GET, I am open to solutions that involve POST submission as ...

What is the best way to search for a CSS selector that includes an attribute beginning with the symbol '@'?

Whenever I want to target an element with a click event, I usually use the following jQuery selector: $('div[@click="login()"]') <div @click="login()"> Login </div> However, when I tried this approach, it resulted ...

Employing multer in conjunction with superagent to enable file uploads from a React application

I am new to using multer and experiencing some difficulties with it. My goal is to upload an image file from a react client using the superagent library to my server. However, the req.file data always shows as undefined in my code: On the server side : ...

Exploring the nesting of client components in Next.jsIf you are

Exploring the realm of NextJS and React, I find myself delving into the realm of client components. One such client component I'm working with is called Form.jsx. It looks something like this: export default function FormHome() { ... a plethora of ...

AJAX/PHP causing delays due to lag problems

I've been trying to implement an asynchronous call in my PHP script, but I keep running into the same issue: "Maximum call stack size exceeded." This is causing severe lag on my site and I suspect there might be a loop somewhere in my code that I just ...

When decoding a JWT, it may return the value of "[object Object]"

Having some trouble decoding a JSON web token that's being sent to my REST API server. I can't seem to access the _id property within the web token. Below is the code I'm currently using: jwt.verify(token, process.env.TOKEN_SECRET, { comp ...

What is the most convenient way to parse a signed cookie in Express/Connect?

When using node.js with Express, I have a question regarding utilizing Connect as the Express sub-component involved. Through socket.io, I receive a signed cookie from the client. The value of this cookie is in the format: s:sessionID.signature Currently ...

Fixing a CSS animation glitch when using JavaScript

I'm facing an unusual issue with my CSS/HTML Check out my code below: a:hover { color: deeppink; transition: all 0.2s ease-out } .logo { height: 300px; margin-top: -100px; transition: all 0.2s ease-in; transform: scale(1) } .logo:hover { transit ...

Is there a simple method to refresh a JSP or Spring MVC page using AJAX?

I'm tackling a seemingly basic question in Java web development here, but I could use some guidance. How can I refresh data on a JSP page? I understand the fundamentals (utilize jQuery for AJAX, Spring MVC for the "Controller" & handle data reque ...

Animation that increments to a predetermined value

I'm trying to create a counter animation that dynamically animates a value calculated by the checkboxes selected. The calculation is working fine, but the animation is not happening. http://jsfiddle.net/dbtj93kL/ $('input[type="checkbox"]&apo ...

Combining login and registration forms on everyauth for express.js

In the standard setup of everyauth for password authentication, login and registration pages are separate. I attempted to combine the respective jade files and adjust the register and login paths in my server file to align. However, upon loading the merged ...

Using Jquery Ajax to Develop Laravel Dropdown Selection with Cascading Feature

I have been working with Laravel 5.6 and encountered an issue with my dropdown selection. Although I selected a province from the dropdown menu, the city menu did not display the corresponding cities. Below is the controller code that I am using: public f ...