Report this

What is the reason for this report?

Getting 403 forbidden when Uploading file to DO spaces using Node + Express

Posted on May 30, 2020

Hi Guys.

I am trying to upload file to DO s3 space using Node + Express. But I am getting 403 forbidden issue.

Following is my uploading source code.

const aws = require('aws-sdk')
const multer = require('multer')
const multerS3 = require('multer-s3')

const space_ep = new aws.Endpoint('nyc3.digitaloceanspaces.com')
const s3 = new aws.S3({
    endpoint: space_ep,
    accessKeyId: 'my-access-key',
    secretAccessKey: 'my-secret-access-key'
})

const uploader = multer({
    storage: multerS3({
        s3: s3,
        bucket: 'my-bucket',
        acl: 'public-read',
        key: function(request, file, cb) {
            console.log(file)
            cb(null, file.originalname)
        }
    })
}).array('upload', 1)

export function upload(req, res, next) {
    uploader(req, res, function(error) {
        if (error) {
            console.log(error)
            return res.redirect('/error')
        }
        console.log('upload success')
        response.redirect('/success')
    })
}

...

let router = require('express').Router()

router.post('/upload', upload)

And when I try to upload, I am getting error in console as following

{ UserSuspended: null
    at Request.extractError (path\node_modules\aws-sdk\lib\services\s3.js:831:35)
    at Request.callListeners (path\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
    at Request.emit (path\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
    at Request.emit (path\node_modules\aws-sdk\lib\request.js:683:14)
    at Request.transition (path\node_modules\aws-sdk\lib\request.js:22:10)
    at AcceptorStateMachine.runTo (path\node_modules\aws-sdk\lib\state_machine.js:14:12)
    at path\node_modules\aws-sdk\lib\state_machine.js:26:10
    at Request.<anonymous> (path\node_modules\aws-sdk\lib\request.js:38:9)
    at Request.<anonymous> (path\node_modules\aws-sdk\lib\request.js:685:12)
    at Request.callListeners (path\node_modules\aws-sdk\lib\sequential_executor.js:116:18)
  message: null,
  code: 'UserSuspended',
  region: null,
  time: 2020-05-30T07:00:19.383Z,
  requestId: 'tx000000000000011987442-005ed20482-21a2fa-nyc3b',
  extendedRequestId: undefined,
  cfId: undefined,
  statusCode: 403,
  retryable: false,
  retryDelay: 17.163587715908,
  storageErrors: [] }

How can I fix this issue? What’s the reason? Please help me.

Regards.



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!

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.

Hello

You can make sure that the used credentials are correct as usually this can be the case for the 403 error.

I’ve used this article as reference when I needed to upload a file to a space product using Node.js

https://www.digitalocean.com/community/tutorials/how-to-upload-a-file-to-object-storage-with-node-js

Hope that this helps!

The developer cloud

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

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.