简体   繁体   English

使用 Node.js 将 zip 文件夹上传到 S3

[英]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.我正在尝试使用 Node.js 将整个本地文件夹上传到 S3 存储桶,现在我的代码可以获取从前面发送的信息,然后根据这些信息对文件进行更改,然后将其上传到 S3 存储桶。 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.我正在尝试使用 Node.js 将整个本地文件夹上传到 S3 存储桶,现在我的代码可以获取从前面发送的信息,然后根据这些信息对文件进行更改,然后将其上传到 S3 存储桶。 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?知道我该怎么做吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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