简体   繁体   中英

Upload a zip folder to S3 using Node.js

I am trying to upload an entire local folder to an S3 bucket using Node.js, for now my code can take the informations sended from the front then make the changes on the file depending on those informations then upload it to S3 bucket. but I want to upload an entire folder with multiple files beside the changed one.

You can find below what I have done so far.

const fs = require('fs'); 
const aws = require('aws-sdk');
aws.config.loadFromPath('./config.json');
const s3 = new aws.S3();
const bucket = 'aws.bucket.name'


const keyUserData = 'UserData'

exports.createFeature = async (req, res, next) => {
  
  let retourUrl = await uploadFile(req.body)

  res.status(201).json(retourUrl)

  

function uploadFile(feature) {
  return new Promise(async (resolve, reject) => {

    //Read file 
    let contents = fs.readFileSync('./controllers/modele/fileName', {encoding:'utf8', flag:'r'});
    

    //change contenant
    contents = contents.replace('regOne', neawValue)
    contents = contents.replace('regTwo', newValue)
    contents = contents.replace('regThree', newValue)

    //Convert file to buffer
    const fileContent = Buffer.from(contents, "utf-8");

    // Setting up S3 upload parameters
    let key = keyUserData+feature.userId+'/fileName'
    const params = {
        Bucket: bucket,
        Key:  key, // File name you want to save as in S3
        Body: fileContent
    };

    // Uploading files to the bucket
    s3.upload(params, function(err, data) {
        if (err) {
            throw err;
        }
        
    });

    const presignedURL = s3.getSignedUrl('getObject', {
      Bucket: bucket,
      Key: key,
      Expires: 60*5
    })
    resolve(presignedURL)
  })


}

Any idea how I can do it?

I am trying to upload an entire local folder to an S3 bucket using Node.js, for now my code can take the informations sended from the front then make the changes on the file depending on those informations then upload it to S3 bucket. but I want to upload an entire folder with multiple files beside the changed one.

You can find below what I have done so far.

const fs = require('fs'); 
const aws = require('aws-sdk');
aws.config.loadFromPath('./config.json');
const s3 = new aws.S3();
const bucket = 'aws.bucket.name'


const keyUserData = 'UserData'

exports.createFeature = async (req, res, next) => {
  
  let retourUrl = await uploadFile(req.body)

  res.status(201).json(retourUrl)

  

function uploadFile(feature) {
  return new Promise(async (resolve, reject) => {

    //Read file 
    let contents = fs.readFileSync('./controllers/modele/fileName', {encoding:'utf8', flag:'r'});
    

    //change contenant
    contents = contents.replace('regOne', neawValue)
    contents = contents.replace('regTwo', newValue)
    contents = contents.replace('regThree', newValue)

    //Convert file to buffer
    const fileContent = Buffer.from(contents, "utf-8");

    // Setting up S3 upload parameters
    let key = keyUserData+feature.userId+'/fileName'
    const params = {
        Bucket: bucket,
        Key:  key, // File name you want to save as in S3
        Body: fileContent
    };

    // Uploading files to the bucket
    s3.upload(params, function(err, data) {
        if (err) {
            throw err;
        }
        
    });

    const presignedURL = s3.getSignedUrl('getObject', {
      Bucket: bucket,
      Key: key,
      Expires: 60*5
    })
    resolve(presignedURL)
  })


}

Any idea how I can do it?

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