Upload a file to Amazon S3 with NodeJS

后端 未结 7 802

I ran into a problem while trying to upload a file to my S3 bucket. Everything works except that my file paramters do not seem appropriate. I am using Amazon S3 sdk to uploa

7条回答
  •  攒了一身酷
    2020-12-04 07:19

    I found the following to be a working solution::

    npm install aws-sdk
    


    Once you've installed the aws-sdk , use the following code replacing values with your where needed.

    var AWS = require('aws-sdk');
    var fs =  require('fs');
    
    var s3 = new AWS.S3();
    
    // Bucket names must be unique across all S3 users
    
    var myBucket = 'njera';
    
    var myKey = 'jpeg';
    //for text file
    //fs.readFile('demo.txt', function (err, data) {
    //for Video file
    //fs.readFile('demo.avi', function (err, data) {
    //for image file                
    fs.readFile('demo.jpg', function (err, data) {
      if (err) { throw err; }
    
    
    
         params = {Bucket: myBucket, Key: myKey, Body: data };
    
         s3.putObject(params, function(err, data) {
    
             if (err) {
    
                 console.log(err)
    
             } else {
    
                 console.log("Successfully uploaded data to myBucket/myKey");
    
             }
    
          });
    
    });
    

    I found the complete tutorial on the subject here in case you're looking for references ::


    How to upload files (text/image/video) in amazon s3 using node.js

提交回复
热议问题