【发布时间】:2019-11-19 11:19:12
【问题描述】:
我不是在询问将文件从浏览器上传到 nodejs 脚本。
但我正在寻找将文件上传到另一台服务器的选项,例如我将在名为 A 的服务器中有一个 nodejs,
我想将文件(/file_path/filename.realm) 上传到名为 B(AWS S3) 的服务器。
【问题讨论】:
标签: javascript node.js amazon-s3 realm
我不是在询问将文件从浏览器上传到 nodejs 脚本。
但我正在寻找将文件上传到另一台服务器的选项,例如我将在名为 A 的服务器中有一个 nodejs,
我想将文件(/file_path/filename.realm) 上传到名为 B(AWS S3) 的服务器。
【问题讨论】:
标签: javascript node.js amazon-s3 realm
为此,您必须使用 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}`);
});
};
【讨论】: