我不是要从浏览器上传文件到nodejs脚本。
但是我正在寻找将文件上传到另一台服务器的选项,例如,我将在名为A的服务器中拥有一个nodejs,
我想将文件(/file_path/filename.realm)上传到名为B(AWS S3)的服务器。
我不是要从浏览器上传文件到nodejs脚本。
但是我正在寻找将文件上传到另一台服务器的选项,例如,我将在名为A的服务器中拥有一个nodejs,
我想将文件(/file_path/filename.realm)上传到名为B(AWS S3)的服务器。
您必须为此使用aws-sdk,然后执行以下操作:
(注意:考虑到您已经访问过AWS S3,并且已经创建了它)
示例代码:
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}`);
});
};