jsreport-aws-s3-storage using environment variables



  • You use the latest jsreport-aws-s3-storage from npm, right?
    Can you share also the jsreport.config.json content?

    So far I cannot replicate this. Based on your logs, the extension gets disabled because blobStorage.provider !== aws-s3-storage



  • Hi, yes it should be the latest version of jsreport-aws-s3-storage.

    Here is the jsreport.config.json content:

    {
      "extensions": {
        "authentication": {
          "cookieSession": {
            "secret": "<your strong secret here>"
          },
          "admin": {
            "username": "admin",
            "password": "password"
          },
          "enabled": false
        },
        "scripts": {
          "timeout": 40000,
          "strategy": "http-server",
          "allowedModules": ["jsreport", "jsrender", "jquery", "request", "express", "aws-sdk", "process", "jsreport-core", "jsreport-aws-s3-storage"]
        },
        "sample-template": {
          "createSamples": false
        },
        "express": {
          "inputRequestLimit": "250mb"
        },
        "assets": {
          "allowedFiles" : "/highcharts/js/themes/**.js"
        }
      },
      "httpPort": 5488,
      "httpsPort": 5489,
      "certificate": {
        "key": "~/.ssh/jsreport.key",
        "cert": "~/.ssh/jsreport.cer"
      },
      "store": {
        "provider": "fs"
      },
      "blobStorage": {
        "provider": "fs"
      },
      "logger": {
        "console": {
          "transport": "console",
          "level": "debug"
        },
        "file": {
          "transport": "file",
          "level": "info",
          "filename": "logs/reporter.log"
        },
        "error": {
          "transport": "file",
          "level": "error",
          "filename": "logs/error.log"
        }
      },
      "allowLocalFilesAccess": true,
      "templatingEngines": {
        "timeout": 10000,
        "strategy": "http-server"
      },
      "chrome": {
        "timeout": 40000
      },
      "phantom": {
        "strategy": "phantom-server",
        "defaultPhantomjsVersion": "2.1.1"
      },
      "freeze": {
        "hardFreeze": false
      }
    }
    

    In my original post, you can see that { blobStorage: { provider: 'aws-s3-storage' } }.



  • Please share the whole init code. The config is fine, the problem will be probably in the init code



  • I'm just using the default server.js built in to jsreports. Unless I'm misunderstanding what you are asking for?



  • You originally posted some code that was not the default built in.

    I mean this is not the built-in

     var jsreport_core = require('jsreport-core') ({ blobStorage: { provider: 'aws-s3-storage' } });
        jsreport_core.use(require('jsreport-aws-s3-storage')({
                "accessKeyId":  process.env.S3_SECRET_ACCESS_KEY,
    			"secretAccessKey":  process.env.S3_ACCESS_KEY_ID,
    			"bucket": process.env.S3_BUCKET_NAME,
    			"s3Options": {}
        }));
    


  • Ah I see what you are asking... here is the file

    const AWS = require('aws-sdk');
    const process = require('process');
    
    
    async function beforeRender(req, res, done) {
        //filter out script execution for phantom header & footer
        if (req.options.isChildRequest)
          return done();
        
        console.log('beforeRender - Building report for payee with Id ' + req.data.header.payeeId + ' for period ' + req.data.header.reportingPeriodFrom + ' - ' + req.data.header.reportingPeriodTo);
        done();
    }
    
    function afterRender(req, res, done) {
        //filter out script execution for phantom header & footer
        if (req.options.isChildRequest)
          return done();
    
        // Create CSV
        prepareStatementCSV(req, res);
        
        // Save pdf to S3 bucket - Method 1
        var jsreport_core = require('jsreport-core') ({ blobStorage: { provider: 'aws-s3-storage' } });
        jsreport_core.use(require('jsreport-aws-s3-storage')({
            "accessKeyId":  process.env.S3_SECRET_ACCESS_KEY,
    		"secretAccessKey":  process.env.S3_ACCESS_KEY_ID,
    		"bucket": process.env.S3_BUCKET_NAME,
    		"s3Options": {}
        }));
    
        console.log("afterRender - Report creation completed successfully.");
        done();
    }
       
    


  • Ah ok, you got it wrong. You are now trying to apply an extension in custom jsreport script...
    What you should do is:

    1. npm install jsreport-aws-s3-storage
    2. alter jsreport.config.json with
    {
    	"blobStorage": {  
    		"provider": "aws-s3-storage"
    	},
    	"extensions": {
    		"aws-s3-storage": {
    			"accessKeyId": "...",
    			"secretAccessKey": "...",
    			"bucket": "...",
    			"s3Options": {...}
    		}
    	}
    }
    

    In case you want to use some custom environment variables, apply them in the server.js

    const jsreport = require('jsreport')({
      blobStorage: { provider: 'aws-s3-storage' },
      extensions: {
        'aws-s3-storage': {
          'accessKeyId': process.env.S3_SECRET_ACCESS_KEY,
          'secretAccessKey': process.env.S3_ACCESS_KEY_ID,
          'bucket': process.env.S3_BUCKET_NAME,
          's3Options': {}
        }
      }
    })
    
    if (process.env.JSREPORT_CLI) {
      module.exports = jsreport
    } else {
      jsreport.init().then(() => {
        // running
      }).catch((e) => {
        // error during startup
        console.error(e.stack)
        process.exit(1)
      })
    }
    
    1. in your render request instruct reports extension to save the output into blob storage
       {
          "template": { "shortid" : "g1PyBkARK" },
          "data" : { ... },
          "options": {
              "reports": { "save": true }
          }
       }
    

    https://jsreport.net/learn/reports
    https://jsreport.net/learn/blob-storages



  • Hmm ok that seems to have fixed some of it! It now loads my keys into aws-s3-storage which is printing out in the logs. But when I log in to AWS, the PDF file is not uploaded to the S3 bucket. I know the bucket name and keys are correct since I am using them elsewhere to upload a CSV successfully and I did double check that {"save": true}. Any other ideas?



  • Share the startup logs and render logs again. This time make just simple render request like this

    {
      "template": {
        "content": "foo",
        "recipe": "html",
        "engine": "none"
      },
      "options": {
         "reports": { "save": true } 
      }
    }
    

    To better know what is going on you can also put a console.log here and here



  • Hi, I'm not sure if I have messed something else up, but this is the error I get when I run what you have requested:

    error: Error when processing render request The authorization header is malformed; the Credential is mal-formed; expecting "<YOUR-AKID>/YYYYMMDD/REGION/SERVICE/aws4_request". AuthorizationHeaderMalformed: The authorization header is malformed; the Credential is mal-formed; expecting "<YOUR-AKID>/YYYYMMDD/REGION/SERVICE/aws4_request".
        at Request.extractError (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/services/s3.js:583:35)
        at Request.callListeners (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
        at Request.emit (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
        at Request.emit (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/request.js:683:14)
        at Request.transition (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/request.js:22:10)
        at AcceptorStateMachine.runTo (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/state_machine.js:14:12)
        at /app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/state_machine.js:26:10
        at Request.<anonymous> (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/request.js:38:9)
        at Request.<anonymous> (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/request.js:685:12)
        at Request.callListeners (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
        at Request.emit (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
        at Request.emit (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/request.js:683:14)
        at Request.transition (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/request.js:22:10)
        at AcceptorStateMachine.runTo (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/state_machine.js:14:12)
        at /app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/state_machine.js:26:10
        at Request.<anonymous> (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/request.js:38:9)
        at Request.<anonymous> (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/request.js:685:12)
        at Request.callListeners (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
        at callNextListener (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/sequential_executor.js:96:12)
        at IncomingMessage.onEnd (/app/node_modules/jsreport-aws-s3-storage/node_modules/aws-sdk/lib/event_listeners.js:294:13)
        at IncomingMessage.emit (events.js:194:15)
        at IncomingMessage.EventEmitter.emit (domain.js:441:20)
    2019-03-25T16:58:04.405Z - error: Error during processing request at https://localhost:5489/api/report
    

    I am walking through the s3.js file now to see if I can figure out where this is breaking.



  • You seem to have your keys switched based on your the very first post

       "accessKeyId":  process.env.S3_SECRET_ACCESS_KEY,
    			"secretAccessKey":  process.env.S3_ACCESS_KEY_ID,
    


  • 😳 Wow. That fixed it! Amazing amazing!

    One last question... When uploading the PDF to S3, it sets random characters for the name. Is there another place I am supposed to be setting the name?

    {
      "template": {
        "name": "Main" 
      },
      "options": {
        "reportName": "myreport",
        "reports": { "save": true }
      },
      "data": {...}
    }
    


  • This is not configurable at this moment. We have it in the backlog
    https://github.com/jsreport/jsreport/issues/458

    You can fork the extension and just change two lines I guess, if you are in that need
    https://github.com/jsreport/jsreport-aws-s3-storage/blob/master/lib/main.js#L48



  • Ok I will consider a couple options, thanks so much for all your help!


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.