Uploading image to s3 using aws sdk












3














I'm currently using AWS Services to create a user profile.



Basically I can already add user details like (name, gender, interests)



I'm using API Gateway to accept the params I send using PostMan, then it the API gateway POST method will send the request to AWS Lambda then I use aws sdk to insert the data to Dynamodb.



What I would like is to upload an image to the user and the file will be saved to s3, then I'll store the url from s3 to dynamodb together with other user details.



My current lambda code is this:



const AWS =  require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'XXXXXX', apiVersion: 'XXX'});
const uuidv4 = require('uuid/v4');

exports.handler = function(event, context, callback) {
const params = {
Item: {
'uuid': { S: "i_" + uuidv4() },
'profileImage': { S: event.profileImage },
'name': { S: event.name }
},
TableName: 'users'
};

dynamodb.putItem(params, function(err, data) {
const response = {
status: 200,
message: JSON.stringify('A record has been created')
};
if (err) {
console.log(err);
callback(err);
} else {
console.log(data);
callback(null, response);
}
});
};


How can I upload an image programatically using the services I mentioned.
The articles I found online is only uploading an image alone and not thru Api Gateway










share|improve this question





























    3














    I'm currently using AWS Services to create a user profile.



    Basically I can already add user details like (name, gender, interests)



    I'm using API Gateway to accept the params I send using PostMan, then it the API gateway POST method will send the request to AWS Lambda then I use aws sdk to insert the data to Dynamodb.



    What I would like is to upload an image to the user and the file will be saved to s3, then I'll store the url from s3 to dynamodb together with other user details.



    My current lambda code is this:



    const AWS =  require('aws-sdk');
    const dynamodb = new AWS.DynamoDB({region: 'XXXXXX', apiVersion: 'XXX'});
    const uuidv4 = require('uuid/v4');

    exports.handler = function(event, context, callback) {
    const params = {
    Item: {
    'uuid': { S: "i_" + uuidv4() },
    'profileImage': { S: event.profileImage },
    'name': { S: event.name }
    },
    TableName: 'users'
    };

    dynamodb.putItem(params, function(err, data) {
    const response = {
    status: 200,
    message: JSON.stringify('A record has been created')
    };
    if (err) {
    console.log(err);
    callback(err);
    } else {
    console.log(data);
    callback(null, response);
    }
    });
    };


    How can I upload an image programatically using the services I mentioned.
    The articles I found online is only uploading an image alone and not thru Api Gateway










    share|improve this question



























      3












      3








      3







      I'm currently using AWS Services to create a user profile.



      Basically I can already add user details like (name, gender, interests)



      I'm using API Gateway to accept the params I send using PostMan, then it the API gateway POST method will send the request to AWS Lambda then I use aws sdk to insert the data to Dynamodb.



      What I would like is to upload an image to the user and the file will be saved to s3, then I'll store the url from s3 to dynamodb together with other user details.



      My current lambda code is this:



      const AWS =  require('aws-sdk');
      const dynamodb = new AWS.DynamoDB({region: 'XXXXXX', apiVersion: 'XXX'});
      const uuidv4 = require('uuid/v4');

      exports.handler = function(event, context, callback) {
      const params = {
      Item: {
      'uuid': { S: "i_" + uuidv4() },
      'profileImage': { S: event.profileImage },
      'name': { S: event.name }
      },
      TableName: 'users'
      };

      dynamodb.putItem(params, function(err, data) {
      const response = {
      status: 200,
      message: JSON.stringify('A record has been created')
      };
      if (err) {
      console.log(err);
      callback(err);
      } else {
      console.log(data);
      callback(null, response);
      }
      });
      };


      How can I upload an image programatically using the services I mentioned.
      The articles I found online is only uploading an image alone and not thru Api Gateway










      share|improve this question















      I'm currently using AWS Services to create a user profile.



      Basically I can already add user details like (name, gender, interests)



      I'm using API Gateway to accept the params I send using PostMan, then it the API gateway POST method will send the request to AWS Lambda then I use aws sdk to insert the data to Dynamodb.



      What I would like is to upload an image to the user and the file will be saved to s3, then I'll store the url from s3 to dynamodb together with other user details.



      My current lambda code is this:



      const AWS =  require('aws-sdk');
      const dynamodb = new AWS.DynamoDB({region: 'XXXXXX', apiVersion: 'XXX'});
      const uuidv4 = require('uuid/v4');

      exports.handler = function(event, context, callback) {
      const params = {
      Item: {
      'uuid': { S: "i_" + uuidv4() },
      'profileImage': { S: event.profileImage },
      'name': { S: event.name }
      },
      TableName: 'users'
      };

      dynamodb.putItem(params, function(err, data) {
      const response = {
      status: 200,
      message: JSON.stringify('A record has been created')
      };
      if (err) {
      console.log(err);
      callback(err);
      } else {
      console.log(data);
      callback(null, response);
      }
      });
      };


      How can I upload an image programatically using the services I mentioned.
      The articles I found online is only uploading an image alone and not thru Api Gateway







      amazon-web-services amazon-s3 aws-lambda






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 10:42

























      asked Nov 21 at 10:10









      Allen Chun

      1,6381937




      1,6381937
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Unfortunately, I am not quite proficient in JS, but I will give you the approach I used in Python to upload an image to S3.



          Since you cannot pass an entire object (like image, zip file or w/e) through API gateway, you have to include it in the payload of your POST.



          An image can be represented as base64 encoded string that you can save to S3. Using the SDK, once the operation is successful, you will recieve the upload address or you can generate yourself (that is quite simple, a lot of guides on the internet).



          As the image in S3 will be just a base64 encoded string by default, you will need to specify the type (Content-Type header) of object you are uploading to S3, so you can open the image afterwards(in a browser for example).






          share|improve this answer





















          • I'm also thinking I need to upload an image directly using my frontend which is Vue/Nuxt directly to s3, since I cannot pass the image using Api Gateway. Thanks for the thoughts
            – Allen Chun
            Nov 22 at 2:14











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409686%2fuploading-image-to-s3-using-aws-sdk%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Unfortunately, I am not quite proficient in JS, but I will give you the approach I used in Python to upload an image to S3.



          Since you cannot pass an entire object (like image, zip file or w/e) through API gateway, you have to include it in the payload of your POST.



          An image can be represented as base64 encoded string that you can save to S3. Using the SDK, once the operation is successful, you will recieve the upload address or you can generate yourself (that is quite simple, a lot of guides on the internet).



          As the image in S3 will be just a base64 encoded string by default, you will need to specify the type (Content-Type header) of object you are uploading to S3, so you can open the image afterwards(in a browser for example).






          share|improve this answer





















          • I'm also thinking I need to upload an image directly using my frontend which is Vue/Nuxt directly to s3, since I cannot pass the image using Api Gateway. Thanks for the thoughts
            – Allen Chun
            Nov 22 at 2:14
















          1














          Unfortunately, I am not quite proficient in JS, but I will give you the approach I used in Python to upload an image to S3.



          Since you cannot pass an entire object (like image, zip file or w/e) through API gateway, you have to include it in the payload of your POST.



          An image can be represented as base64 encoded string that you can save to S3. Using the SDK, once the operation is successful, you will recieve the upload address or you can generate yourself (that is quite simple, a lot of guides on the internet).



          As the image in S3 will be just a base64 encoded string by default, you will need to specify the type (Content-Type header) of object you are uploading to S3, so you can open the image afterwards(in a browser for example).






          share|improve this answer





















          • I'm also thinking I need to upload an image directly using my frontend which is Vue/Nuxt directly to s3, since I cannot pass the image using Api Gateway. Thanks for the thoughts
            – Allen Chun
            Nov 22 at 2:14














          1












          1








          1






          Unfortunately, I am not quite proficient in JS, but I will give you the approach I used in Python to upload an image to S3.



          Since you cannot pass an entire object (like image, zip file or w/e) through API gateway, you have to include it in the payload of your POST.



          An image can be represented as base64 encoded string that you can save to S3. Using the SDK, once the operation is successful, you will recieve the upload address or you can generate yourself (that is quite simple, a lot of guides on the internet).



          As the image in S3 will be just a base64 encoded string by default, you will need to specify the type (Content-Type header) of object you are uploading to S3, so you can open the image afterwards(in a browser for example).






          share|improve this answer












          Unfortunately, I am not quite proficient in JS, but I will give you the approach I used in Python to upload an image to S3.



          Since you cannot pass an entire object (like image, zip file or w/e) through API gateway, you have to include it in the payload of your POST.



          An image can be represented as base64 encoded string that you can save to S3. Using the SDK, once the operation is successful, you will recieve the upload address or you can generate yourself (that is quite simple, a lot of guides on the internet).



          As the image in S3 will be just a base64 encoded string by default, you will need to specify the type (Content-Type header) of object you are uploading to S3, so you can open the image afterwards(in a browser for example).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 12:18









          AlexK

          824413




          824413












          • I'm also thinking I need to upload an image directly using my frontend which is Vue/Nuxt directly to s3, since I cannot pass the image using Api Gateway. Thanks for the thoughts
            – Allen Chun
            Nov 22 at 2:14


















          • I'm also thinking I need to upload an image directly using my frontend which is Vue/Nuxt directly to s3, since I cannot pass the image using Api Gateway. Thanks for the thoughts
            – Allen Chun
            Nov 22 at 2:14
















          I'm also thinking I need to upload an image directly using my frontend which is Vue/Nuxt directly to s3, since I cannot pass the image using Api Gateway. Thanks for the thoughts
          – Allen Chun
          Nov 22 at 2:14




          I'm also thinking I need to upload an image directly using my frontend which is Vue/Nuxt directly to s3, since I cannot pass the image using Api Gateway. Thanks for the thoughts
          – Allen Chun
          Nov 22 at 2:14


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409686%2fuploading-image-to-s3-using-aws-sdk%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python