繁体   English   中英

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

[英]Upload a zip folder to S3 using Node.js

我正在尝试使用 Node.js 将整个本地文件夹上传到 S3 存储桶,现在我的代码可以获取从前面发送的信息,然后根据这些信息对文件进行更改,然后将其上传到 S3 存储桶。 但我想上传一个包含多个文件的整个文件夹,除了更改的文件。

你可以在下面找到我到目前为止所做的事情。

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)
  })


}

知道我该怎么做吗?

我正在尝试使用 Node.js 将整个本地文件夹上传到 S3 存储桶,现在我的代码可以获取从前面发送的信息,然后根据这些信息对文件进行更改,然后将其上传到 S3 存储桶。 但我想上传一个包含多个文件的整个文件夹,除了更改的文件。

你可以在下面找到我到目前为止所做的事情。

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)
  })


}

知道我该怎么做吗?

暂无
暂无

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

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