Append data to an S3 object

前端 未结 6 1020
长情又很酷
长情又很酷 2020-12-05 01:34

Let\'s say that I have a machine that I want to be able to write to a certain log file stored on an S3 bucket.

So, the machine needs to have writing abilities to tha

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 02:11

    I had the similar issue and this is what I had asked

    how to Append data in file using AWS Lambda

    Here's What I come up with to solve the above problem:

    Use getObject to retrive from the existing file

       s3.getObject(getParams, function(err, data) {
       if (err) console.log(err, err.stack); // an error occurred
       else{
           console.log(data);           // successful response
           var s3Projects = JSON.parse(data.Body);
           console.log('s3 data==>', s3Projects);
           if(s3Projects.length > 0) {
               projects = s3Projects;
           }   
       }
       projects.push(event);
       writeToS3(); // Calling function to append the data
    });
    

    Write function to append in the file

       function writeToS3() {
        var putParams = {
          Body: JSON.stringify(projects),
          Bucket: bucketPath, 
          Key: "projects.json",
          ACL: "public-read"
         };
    
        s3.putObject(putParams, function(err, data) {
           if (err) console.log(err, err.stack); // an error occurred
           else     console.log(data);           // successful response
            callback(null, 'Hello from Lambda');
         });
    }
    

    Hope this help!!

提交回复
热议问题