简体   繁体   中英

Uploading a url image to s3 aws

I'm trying to upload an image but i keep getting an error with the export of the secret and key SyntaxError: Unexpected reserved word . here is what i've tried

first i require the aws dependencies.

var AWS = require('aws-sdk');
export AWS_ACCESS_KEY_ID='key'
export AWS_SECRET_ACCESS_KEY='secret'
AWS.config.region = 'eu-west-1';¨

Upload image function

function uploadImage(url, title) {

  request(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {

      var s3Bucket = new AWS.S3( { params: {Bucket: 'bucket'} } )
      var data = {Key: title + "-" + Math.floor(Date.now() / 1000), Body: body};
      s3Bucket.putObject(data, function(err, data){
        if (err) {
            console.log('Error uploading data: ', data);
          } else {
            console.log('succesfully uploaded the image!');
          }
      });

    }
  })


}

run the function

uploadImage("http:/imageUrl", "test");

export is not a correct way to add an environmental variable in node.js.

You should either

process.env.AWS_ACCESS_KEY_ID='key'
...
var AWS = require('aws-sdk');

or, most likely, pass them to your script as

export AWS_ACCESS_KEY_ID='key'
node server.js

export is a reserved js word.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM