简体   繁体   中英

Upload realm file from realm server to AWS S3 bucket in NodeJS

I'm not asking about uploading a file from a browser to a nodejs script.

But I am looking for the option to upload the file to another server, example I will have a nodejs in server called A,

I want to upload the file(/file_path/filename.realm) to Server called B(AWS S3).

You have to use aws-sdk for this purpose and you follow:

(Note: Considering that you have access to AWS S3 and already created it)

  1. initiate a new instance of S3.
  2. read content from a file (realm file) with the fs module.
  3. Assign the contents of the realm file to Body of upload params.
  4. call the upload function.

sample code:

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

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