1
votes

I am trying to create the folder in the s3 bucket using the AWS-SDK from the node. I have full access to the respective bucket However I am always getting "Access Denied when I run the code to create a folder.

 function init() {
  if (s3Instance === null) {
    AWS.config.update({
      accessKeyId: S3_ACCESS_KEY_ID,
      secretAccessKey: S3_SECRET_ACCESS_KEY,
      region: S3_REGION
    });

    if (HTTP_PROXY) {
      AWS.config.update({
        httpOptions: { agent: proxy(HTTP_PROXY) }
      });
    }

    // s3Instance = new AWS.S3();
    s3Instance = new AWS.S3({ apiVersion: "2006-03-01" });
  }
}

function createFolder(folderKey) {
  return new Promise((resolve, reject) => {
    var params = {
      Bucket: S3_BUCKET,
      Key: `${folderKey}/`,
      Body: "body does not matter"
    };
    s3Instance.upload(params, function (err, data) {
      if (err) {
        console.log(err, err.stack);
        reject(err);

      } else {
        console.log("Successfully created a folder on S3");
        resolve("Successfully created a folder on S3");
      }
    });
  });
}

null: Error: Access Denied at Request.extractError (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\services\s3.js:837:35) at Request.callListeners (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\sequential_executor.js:106:20) at Request.emit (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\sequential_executor.js:78:10) at Request.emit (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\request.js:688:14) at Request.transition (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\request.js:22:10) at AcceptorStateMachine.runTo (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\state_machine.js:14:12) at c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\state_machine.js:26:10 at Request. (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\request.js:38:9) at Request. (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\request.js:690:12) at Request.callListeners (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\sequential_executor.js:116:18) at Request.emit (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\sequential_executor.js:78:10) at Request.emit (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\request.js:688:14) at Request.transition (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\request.js:22:10) at AcceptorStateMachine.runTo (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\state_machine.js:14:12) at c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\state_machine.js:26:10 at Request. (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\request.js:38:9) at Request. (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\request.js:690:12) at Request.callListeners (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\sequential_executor.js:116:18) at callNextListener (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\sequential_executor.js:96:12) at IncomingMessage.onEnd (c:\Users\sunny.bogawat\Workspace\oms-myportal\gateway\app\node_modules\aws-sdk\lib\event_listeners.js:313:13) at IncomingMessage.emit (<node_internals>/events.js:187:15) at IncomingMessage.EventEmitter.emit (<node_internals>/domain.js:441:20) at endReadableNT (<node_internals>/_stream_readable.js:1094:12) at process._tickCallback (internal/process/next_tick.js:63:19) {message: 'Access Denied', code: 'AccessDenied', region: null, time: Wed Oct 14 2020 16:05:15 GMT+0530 (India Standard Time), requestId: 'A0B8B6F506364753', …}

1
Is the app running under your IAM user, or some instance role?Marcin
It's running as an IAM userSunny

1 Answers

0
votes

I think you should use putObject and try to create your s3 object like this.

s3 = new S3({
    credentials: {
        region: process.env.AWS_REGION,
        accessKeyId: process.env.AWS_ACCESS_KEY_ID,
        secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    }
});

    const uploadObject = async (bucket, key, body) => {
    try {

        const params = {
            Bucket: bucket,
            Key: key,
            Body: body//,
            //ContentType: "application/pdf"
        }

        var data = await s3.putObject(params).promise();

        return { message: data.error || 'Successfull' };



    } catch (err) {
        console.log(err)
        return { message: err.toString() || 'Failed' };
    }

};