使用nodejs将领域文件从领域服务器上传到s3

我不是要从浏览器上传文件到nodejs脚本。

但是我正在寻找将文件上传到另一台服务器的选项,例如,我将在名为A的服务器中拥有一个nodejs,

我想将文件(/file_path/filename.realm)上传到名为B(AWS S3)的服务器。

beibeiji 回答:使用nodejs将领域文件从领域服务器上传到s3

您必须为此使用aws-sdk,然后执行以下操作:

(注意:考虑到您已经访问过AWS S3,并且已经创建了它)

  1. 启动S3的新实例。
  2. 使用fs模块从文件(领域文件)中读取内容。
  3. 将领域文件的内容分配给上载参数正文。
  4. 调用上传功能。

示例代码:

const fs = require('fs');
const AWS = require('aws-sdk');

const s3 = new AWS.S3({
accessKeyId: <awsS3AccessId>,// access Id of your bucket
secretAccessKey:<awsS3SecretKey>,// secret key of your bucket
 });

const uploadFile = (fileName) => {
    // Read content from the file
    const fileContent = fs.readFileSync(fileName);

    // Setting up S3 upload parameters
    const params = {
        Bucket: BUCKET_NAME,Key: fileName,// 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;
        }
        console.log(`File uploaded successfully. ${data.Location}`);
    });
};
本文链接:https://www.f2er.com/3074037.html

大家都在问