1

在我的serverless应用程序中,我想创建动态生成的 pdf,然后将创建的 pdf 上传到aws s3. 我的问题是,当一个 url 从服务器返回到客户端代码时,上传的 url 不起作用。我的代码如下:

客户端 javascript 代码 (angular.js)

 $scope.downloadAsPDF = function() {

    // first I have to sent all html data into server
    var html = angular.element('html').html(); // get all page data

    var service = API.getService();
    service.downloadPdf({}, { html : html },   // api call with html data
      function(res) {
        console.log("res : ", res);
        window.open(res.url);   // open uploaded pdf file
                               // err: The server replies that you don't have permissions to download this file
                               // HTTP/1.1 403 Forbidden
      }, function(err) {
        console.log("err : ", err);
      });
  };

无服务器代码

var fs = require('fs');
var pdf = require('html-pdf');

var AWS = require("aws-sdk");
var s3 = new AWS.S3();

module.exports.handler = function(event, context) {

    if (event.html) {   // client html data

    AWS.config.update({
        accessKeyId:  'xxxxxxxxxxxxxxxxx',
        secretAccessKey:  'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        region: 'xxxxxxxxxxxxxxxxxxxx'
    });

    var awsInfo = {
        bucket: 'xxxxx-xxxxxx'
    };

    var baseUrl = 'https://s3-my-region.amazonaws.com/s3-upload-directory';
    var folderRoot = 'development/pdf';

    // unique file name
    var output_filename = Math.random().toString(36).slice(2) + '.pdf';

    // file created directory
    var output = '/tmp/' + output_filename; 

    pdf.create(event.html, options).toStream(function(err, stream) {

       if( err ) {
           console.log('pdf err : ', err);
       } else {

            writeStream =fs.createWriteStream(output);

             s3.putObject({
                Bucket : awsInfo.bucket,
                Key : folderRoot + '/' + output_filename,
                Body :  fs.createReadStream(output),
                ContentType : "application/pdf"
             },
             function(error, data) {

                if (error != null) {
                   console.log("error: " + error);
                } else {
                    // upload data:  { ETag: '"d41d8cd98f00b204e9800998ecf8427e"' }
                    console.log('upload data : ', data);

                   return cb(null, {
                       // return actual aws link, but no file
                       // ex: 'https://s3-my-region.amazonaws.com/s3-upload-directory/output_filename.pdf
                       url:  baseUrl +  '/' + output_filename
                   });
                }

             });

         }
    }
};
4

3 回答 3

3

我已经解决了我的问题。在生成pdf之前,我试图上传pdf。我已经使用以下代码解决了这个问题:

  pdf.create(event.html, options).toStream(function(err, stream) {
      if (err) {
          console.log('pdf err : ', err);
      } else {
           var stream = stream.pipe(fs.createWriteStream(output));

          stream.on('finish', function () {

            s3.putObject({
                  Bucket : awsInfo.bucket,
                  Key : folderRoot + '/' + output_filename,
                  Body :  fs.createReadStream(output),
                  ContentType : "application/pdf"
                },
                function(error, data) {

                  if (error != null) {
                    console.log("error: " + error);
                    return cb(null, {
                      err:  error
                    });
                  } else {

                    var url =  baseUrl +  '/' + output_filename

                    return cb(null, {
                      url: url
                    });

                  }

                });

          });
      }
  });
于 2016-06-03T18:00:19.323 回答
0

如果我们不想在 s3 上传,只需从 aws-lambda 返回生成的文件。

于 2016-06-20T09:00:06.583 回答
0

我以前做过类似的事情。
我想从你那里得到一些澄清,然后我将能够更好地帮助你。
1)在您的代码(服务器端)中,您在回调函数中提到实际的 aws 链接正在返回。您确定您的文件正在上传到 Amazon s3。我的意思是您是否检查了您的存储桶中的文件?
2)您是否在 Amazon s3 上设置了任何自定义存储桶策略。存储桶策略在可以从 S3 下载的内容中发挥重要作用。
3)您是否检查了日志以确切了解导致错误的代码部分?请向我提供此信息,我认为我应该能够帮助您。

于 2016-06-03T11:48:15.643 回答