简体   繁体   中英

Unable to upload file on amazon s3

I have given all s3 access to lambda to upload file on s3. There is no error and no response from aws.All the permission are given.

    var params4 = {
        Bucket: "bcket name",
        Key         : "tt.pdf",
        Body        : buf,
        ContentType : 'application/pdf',
        ACL       : 'public-read',
        ContentEncoding: 'base64',
    }
                                        
    var data1= s3.putObject(params4, function(err, data){
     console.log("successful");
     if (err) { 
      console.log("putobbject="+err);   
     }else{
      console.log(data);
     }
    });

I guess you're using the 'aws-sdk' node package, right?

This is the code I'm using. The file type should be detected automatically. I used it for CSV and JSON so far but should work fine for PDF as well.

 const fs = require('fs'); const AWS = require('aws-sdk'); const uploadFile = (localFile, s3File, bucket) => { return new Promise((resolve, reject) => { const { AWS_ACCESS_KEY, AWS_SECRET_KEY } = process.env; const s3 = new AWS.S3({ accessKeyId: AWS_ACCESS_KEY, secretAccessKey: AWS_SECRET_KEY }); const loadedFile = fs.readFileSync(localFile); const params = { Bucket: bucket, Key: s3File, Body: loadedFile }; return s3.upload(params, (error) => { if (error) { return reject(error); } return resolve(true); }); }); }; module.exports = uploadFile;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM