Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for AWS_PROFILE #12

Open
FLemon opened this issue May 8, 2018 · 3 comments
Open

support for AWS_PROFILE #12

FLemon opened this issue May 8, 2018 · 3 comments

Comments

@FLemon
Copy link

FLemon commented May 8, 2018

hi, do you have plan to support AWS_PROFILE as alternative to key_id & access_key?

@jdrydn
Copy link

jdrydn commented Feb 13, 2019

@FLemon Since this issue is still open a year later I thought I would share my solution to this using AWS.config.getCredentials():

const AWS = require('aws-sdk');
const S3BrowserDirectUpload = require('s3-browser-direct-upload');

AWS.config.getCredentials(err => {
  if (err) {
    throw err;
  }

  const { accessKeyId, secretAccessKey } = AWS.config.credentials;
  const s3 = new S3BrowserDirectUpload({
    accessKeyId,
    secretAccessKey,
    region: 'us-east-1',
  });

  s3.uploadPostForm({
    bucket: 'mybucket-dev',
    region: 'eu-west-2',
    key: 'fsociety.txt',
    extension: 'txt',
    acl: 'private',
    contentLength: 1024,
  }, (err, data) => {
    if (err) {
      throw err;
    }

    console.log(data);
    // { params:
    //   { key: 'fsociety.txt',
    //     acl: 'private',
    //     'x-amz-algorithm': 'AWS4-HMAC-SHA256',
    //     'x-amz-credential': '### ### ### ###',
    //     'x-amz-date': '### ### ### ###',
    //     policy: '### ### ### ###',
    //     'x-amz-signature': '### ### ### ###',
    //     'content-type': 'text/plain' },
    //   public_url: 'https://mybucket-dev.s3.amazonaws.com/fsociety.txt',
    //   form_url: 'https://mybucket-dev.s3.amazonaws.com/' }
  });
});

Since the above code just uses credentials just like internal AWS SDKs do, this works for AWS_PROFILE, IAM roles, and the rest!

@jdrydn
Copy link

jdrydn commented May 10, 2019

Update: If you're using an IAM role with this, you'll need to include the sessionToken as thus:

const AWS = require('aws-sdk');
const S3BrowserDirectUpload = require('s3-browser-direct-upload');

AWS.config.getCredentials(err => {
  if (err) {
    throw err;
  }

  const { accessKeyId, secretAccessKey, sessionToken } = AWS.config.credentials;
  const s3 = new S3BrowserDirectUpload({
    accessKeyId,
    secretAccessKey,
    region: 'us-east-1',
  });

  s3.uploadPostForm({
    bucket: 'mybucket-dev',
    region: 'eu-west-2',
    key: 'fsociety.txt',
    extension: 'txt',
    acl: 'private',
    contentLength: 1024,
    conditionMatching: [
        { 'x-amz-security-token': sessionToken }
    ],
  }, (err, data) => {
    if (err) {
      throw err;
    }

    data.params['x-amz-security-token'] = sessionToken;

    console.log(data);
    // { params:
    //   { key: 'fsociety.txt',
    //     acl: 'private',
    //     'x-amz-algorithm': 'AWS4-HMAC-SHA256',
    //     'x-amz-credential': '### ### ### ###',
    //     'x-amz-security-token': '### ### ### ###',
    //     'x-amz-date': '### ### ### ###',
    //     policy: '### ### ### ###',
    //     'x-amz-signature': '### ### ### ###',
    //     'content-type': 'text/plain' },
    //   public_url: 'https://mybucket-dev.s3.amazonaws.com/fsociety.txt',
    //   form_url: 'https://mybucket-dev.s3.amazonaws.com/' }
  });
});

Even though "sessionToken" is supported by the constructor in this library, it checks that the sessionToken is an instance of AWS.config, rather than a string, and therefore cannot be used 🤦‍♂

@jdrydn
Copy link

jdrydn commented May 13, 2021

... Anyone coming here from Google:

Use s3.createPresignedPost. Native S3 implementation, so naturally supports AWS_PROFILE out-of-the-box!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants