【问题标题】:Upload realm file from realm server to AWS S3 bucket in NodeJS将领域文件从领域服务器上传到 NodeJS 中的 AWS S3 存储桶
【发布时间】:2019-11-19 11:19:12
【问题描述】:

我不是在询问将文件从浏览器上传到 nodejs 脚本。

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

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

【问题讨论】:

    标签: javascript node.js amazon-s3 realm


    【解决方案1】:

    为此,您必须使用 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}`);
        });
    };
    

    【讨论】:

      猜你喜欢
      • 2019-10-23
      • 2018-06-02
      • 2021-05-10
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多