Passing file in S3 to function Lambda

0

I was wondering if anyone could help me with this issue. Basically, I'm using the library kml2geojson to convert a .KML file to .geoJSON in Python. The kml2geojson library has a function called kml2geojson.main.convert(filepath, directorypath) which takes the .KML file that needs to be converted and the directory it should put the new .geoJSON file.

I can do this fine in my machine, but I'm not sure how I'd do that using Lambda and S3. Suppose the name of the file in the S3 bucket is test.kml and the name of the bucket is bucketkmltest. How would I write the file path and the directory path (bucket path) in the function parameters?

Many thanks in advance,

Gabriel

Edited by: GabrielP on Dec 12, 2020 12:02 AM

Edited by: GabrielP on Dec 12, 2020 12:15 AM

질문됨 3년 전543회 조회
1개 답변
0

Here’s some example code showing get and put from S3 in Lambda

// dependencies
const AWS = require('aws-sdk');
const util = require('util');
const sharp = require('sharp');

// get reference to S3 client
const s3 = new AWS.S3();

exports.handler = async (event, context, callback) => {

// Read options from the event parameter.  
console.log("Reading options from event:\n", util.inspect(event, {depth: 5}));  
const srcBucket = event.Records\[0].s3.bucket.name;  
// Object key may have spaces or unicode non-ASCII characters.  
const srcKey    = decodeURIComponent(event.Records\[0].s3.object.key.replace(/\+/g, " "));  
const dstBucket = srcBucket + "-resized";  
const dstKey    = "resized-" + srcKey;  

// Infer the image type from the file suffix.  
const typeMatch = srcKey.match(/\.(\[^.]*)$/);  
if (!typeMatch) {  
    console.log("Could not determine the image type.");  
    return;  
}  

// Check that the image type is supported    
const imageType = typeMatch\[1].toLowerCase();  
if (imageType != "jpg" && imageType != "png") {  
    console.log(`Unsupported image type: ${imageType}`);  
    return;  
}  

// Download the image from the S3 source bucket.   

try {  
    const params = {  
        Bucket: srcBucket,  
        Key: srcKey  
    };  
    var origimage = await s3.getObject(params).promise();  

} catch (error) {  
    console.log(error);  
    return;  
}    

// set thumbnail width. Resize will set the height automatically to maintain aspect ratio.  
const width  = 200;  

// Use the Sharp module to resize the image and save in a buffer.  
try {   
    var buffer = await sharp(origimage.Body).resize(width).toBuffer();  
          
} catch (error) {  
    console.log(error);  
    return;  
}   

// Upload the thumbnail image to the destination bucket  
try {  
    const destparams = {  
        Bucket: dstBucket,  
        Key: dstKey,  
        Body: buffer,  
        ContentType: "image"  
    };  

    const putResult = await s3.putObject(destparams).promise();   
      
} catch (error) {  
    console.log(error);  
    return;  
}   
      
console.log('Successfully resized ' _ srcBucket _ '/' _ srcKey _  
    ' and uploaded to ' _ dstBucket _ '/' + dstKey);   

};

답변함 3년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠