Vishnu
Vishnu

Reputation: 755

How to verify AWS S3 bucket details is valid or not using Nodejs?

In my application am having a form, in that user should enter their AWS S3 details. Before saving the details I have to check whether the account is correct or not. I am building this application using Node js,mongodb,Angular js.

UPDATE aws s3 details are

{
       "aws": {
        "key": "",
        "secretkey": "",
        "region": "",
        "bucket_name": ""
    }

Upvotes: 0

Views: 4651

Answers (3)

sidhuko
sidhuko

Reputation: 3376

Using the AWS Javascript SDK you should make a headBucket request using the supplied credentials in the constructor.

const AWS = require('aws-sdk');
const s3 = new AWS.S3({ credentials, region });

s3.headBucket({
  Bucket: "examplebucket"
}, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
})

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#headBucket-property

If the request is successful you have access.

Upvotes: 3

Yalamber
Yalamber

Reputation: 7580

You can try making a s3 client using aws s3 sdk and then try calling method that your app needs to. For example if you need to create an object in the provided bucket, Then simply try making test object and see if it works. You can pass stored credentials like in below links: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-json-file.html

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html

const AWS = require('aws-sdk');
const fs = require('fs');

AWS.config.update({ accessKeyId: '...', secretAccessKey: '...' });
//make sure you pass proper credentials
const s3 = new AWS.S3();
fs.readFile('test-file.txt', function (err, data) {
  if (err) { throw err; }
  var base64data = new Buffer(data, 'binary');
  const params = {
    Bucket: '...bucket-name-here...',
    Key: 'test-object.txt',
    Body: base64data
  };
  s3.putObject(params, function(err, data) {
    //check error or data to see if you have proper permissions.
  });
});

Also see these examples for putObject method: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property https://gist.github.com/homam/8646090

Upvotes: 0

abdulbari
abdulbari

Reputation: 6242

If you have the user credentials then you can use aws-sdk which is available as node module.

set the credentials in sdk config and use S3 API to verify the given buckets.

npm install aws-sdk

var AWS = require('aws-sdk');
AWS.config = new AWS.Config();
AWS.config.accessKeyId = "accessKey";
AWS.config.secretAccessKey = "secretKey";
AWS.config.region = "region";

var s3 = new AWS.S3();

You can refer the site for better understanding

Upvotes: 0

Related Questions