2015-09-27 89 views
3

我一直在嘗試創建一個AWS節點lambda函數來從S3下載PDF文件,爲該文件的第一頁生成縮略圖並將該縮略圖上傳回S3。由於我不是專家,我試圖從AWS提供的Lambda示例中激發靈感來調整圖像大小,以及在node.js中的PDF縮略圖生成器的SO上找到的node.js,但無法使其工作。 從S3下載工程,上傳回S3工作,但縮略圖生成失敗。 下面看我的代碼:創建一個lambda函數以生成PDF文件縮略圖

// Download the pdf from S3, create thumbnail, and upload to cache. 
 
\t async.waterfall([ 
 
\t \t function download(next) { 
 
\t \t \t // Download the pdf from S3 into a buffer. 
 
\t \t \t s3.getObject({ 
 
\t \t \t \t \t Bucket: BUCKET, 
 
\t \t \t \t \t Key: pdfkey 
 
\t \t \t \t }, 
 
\t \t \t \t next); 
 
\t \t \t }, 
 
\t \t function thumbnail(response, next) { 
 
\t \t \t gm(response.Body[0]).size(function(err, size) { 
 
\t \t \t \t // Transform the image buffer in memory. 
 
\t \t \t \t this.resize(requestedwidth, requestedheight).toBuffer(format.toUpperCase(), function(err, buffer) { 
 
\t \t \t \t \t if (err) { 
 
\t \t \t \t \t \t console.log('failed generating thumbnail'); 
 
\t \t \t \t \t \t next(err); 
 
\t \t \t \t \t } else { 
 
\t \t \t \t \t \t next(null, response.ContentType, buffer); 
 
\t \t \t \t \t } 
 
\t \t \t \t }); 
 
\t \t \t }); 
 
\t \t }, 
 
\t \t function upload(contentType, data, next) { 
 
\t \t \t // Stream the thumbnail 
 
\t \t \t s3.putObject({ 
 
\t \t \t \t \t Bucket: BUCKET, 
 
\t \t \t \t \t Key: thumbnailkey, 
 
\t \t \t \t \t ACL:"public-read", 
 
\t \t \t \t \t Body: data, 
 
\t \t \t \t \t ContentType: contentType 
 
\t \t \t \t }, 
 
\t \t \t \t next); 
 
\t \t \t } 
 
\t \t ], function (err) { 
 
\t \t \t if (err) { 
 
\t \t \t \t context.fail(new Error(
 
\t \t \t \t \t 'Unable to create thumbnail for ' + BUCKET + '/' + pdfkey + 
 
\t \t \t \t \t ' and upload to ' + BUCKET + '/' + thumbnailkey + 
 
\t \t \t \t \t ' due to an error: ' + err 
 
\t \t \t \t)); 
 
\t \t \t } else { 
 
\t \t \t \t context.succeed(
 
\t \t \t \t \t 'Successfully resized ' + BUCKET + '/' + pdfkey + 
 
\t \t \t \t \t ' and uploaded to ' + BUCKET + '/' + thumbnailkey 
 
\t \t \t \t); 
 
\t \t \t } 
 
\t \t } 
 
\t);

任何幫助將不勝感激!

+0

你有沒有得到這個想通了?希望做同樣的事情。乾杯 –

回答

3

下面是結束工作的代碼。有以下4個庫下面的代碼在節點模塊:

  • 異步
  • mktemp的
  • PDF圖像

var async = require('async'); 
var AWS = require('aws-sdk'); 
var gm = require('gm').subClass({ imageMagick: true }); // Enable ImageMagick integration. 
var util = require('util'); 
var fs = require('fs'); 
var mktemp = require("mktemp"); 

var BUCKET = "XXXXXXXXXX"; 

var s3 = new AWS.S3(); 
exports.handler = function(event, context) { 

var pdfkey = decodeURIComponent(event.pdfkey.replace(/\+/g, " ")); 
var thumbnailkey = decodeURIComponent(event.thumbnailkey.replace(/\+/g, " ")); 
var requestedwidth = event.width; 
var requestedheight = event.height; 
var shape = event.shape; 
var format = event.format; 

// Infer the pdf type. 
var typeMatch = pdfkey.match(/\.([^.]*)$/); 
if (!typeMatch) { 
    context.fail(new Error('unable to infer pdf type for key ' + pdfkey)); 
    return; 
} 
var fileType = typeMatch[1]; 
if (fileType != "pdf") { 
    context.fail(new Error('skipping non-pdf ' + pdfkey)); 
    return; 
} 

// Download the pdf from S3, create thumbnail, and upload to cache. 
async.waterfall([ 
    function download(next) { 
     // Download the pdf from S3 into a buffer. 
     s3.getObject({ 
      Bucket: BUCKET, 
      Key: pdfkey 
     }, 
     next); 
    }, 
    function thumbnail(response, next) { 
     console.log('generating thumbnail'); 
     var temp_file, image; 

     temp_file = mktemp.createFileSync("/tmp/XXXXXXXXXX.pdf"); 
     fs.writeFileSync(temp_file, response.Body); 
     image = gm(temp_file + "[0]").flatten().colorspace("CMYK"); 

     image.size(function(err, size) { 
      if ((requestedwidth > 0) && (requestedheight > 0)) 
      { 

       if (shape == "pad") 
       { 
        // Transform the image buffer in memory. 
        this.resize(requestedwidth, requestedheight).gravity('Center').background('transparent').extent(requestedwidth, requestedheight) 
        .toBuffer(format.toUpperCase(), function(err, buffer) { 
         if (err) { 
          next(err); 
         } else { 
          next(null, response.ContentType, buffer); 
         } 
        }); 
       } 
       else 
       { 
        // Transform the image buffer in memory. 
        this.resize(requestedwidth, requestedheight) 
        .toBuffer(format.toUpperCase(), function(err, buffer) { 
         if (err) { 
          next(err); 
         } else { 
          next(null, response.ContentType, buffer); 
         } 
        }); 
       } 
      } 
      else 
      { 
       if (requestedwidth > 0) 
       { 
        // Transform the image buffer in memory. 
        this.resize(requestedwidth) 
        .toBuffer(format.toUpperCase(), function(err, buffer) { 
         if (err) { 
          next(err); 
         } else { 
          next(null, response.ContentType, buffer); 
         } 
        }); 

       } 
       else 
       { 
        // Transform the image buffer in memory. 
        this.resize(null, requestedheight) 
        .toBuffer(format.toUpperCase(), function(err, buffer) { 
         if (err) { 
          next(err); 
         } else { 
          next(null, response.ContentType, buffer); 
         } 
        }); 
       } 
      } 
     }); 
    }, 
    function upload(contentType, data, next) { 
     // Stream the thumbnail 
     console.log('uploading thumbnail'); 
     s3.putObject({ 
      Bucket: BUCKET, 
      Key: thumbnailkey, 
      ACL:"public-read", 
      Body: data, 
      ContentType: "image/" + format 
     }, 
     next); 
    } 
    ], function (err) { 
     if (err) { 
      context.fail(new Error(
       'Unable to create thumbnail for ' + BUCKET + '/' + pdfkey + 
       ' and upload to ' + BUCKET + '/' + thumbnailkey + 
       ' due to an error: ' + err 
       )); 
     } else { 
      context.succeed(
       'Successfully resized ' + BUCKET + '/' + pdfkey + 
       ' and uploaded to ' + BUCKET + '/' + thumbnailkey 
       ); 
     } 
    } 
    ); 
};