直接创建文件并将其上传到S3存储桶目录

时间:2019-04-08 19:08:53

标签: javascript node.js amazon-s3

我编写了创建多个HTML文件的代码。我正在尝试将这些文件直接上传到S3存储桶目录,而不将文件保存在本地。该怎么办?

我一直在用fs.writeFileSync(directory + title + '.html', data);将文件保存在本地,我想我必须按照将data传递到fs流中的方式来做一些事情,但是我不知道不知道从哪里开始。

如果有人能帮助我并指出正确的方向,我将非常感激。谢谢!

编辑:即使当我尝试上传由我的代码生成并保存在本地的html文件时,它似乎也没有尝试上传该文件。这是我的代码的上传部分:

s3.upload(bucket, function(err, data){
                    if(err) {
                        console.log("Error: ", err);
                    } else {
                        console.log("Success: ", data.Location);
                    }
                });

但是,它不会显示错误或成功消息。为什么会这样?

编辑2:这是我正在做的要点:

let aws = require('aws-sdk');

aws.config.update({
    //Removed the strings for security reasons
    accessKeyId: '*******',
    secretAccessKey: '********'
});

let s3 = new aws.S3({
    apiVersion: "2006-03-01",
});

//This lists all the buckets.  It works correctly!
s3.listBuckets(function(err, data){
    if(err){
        console.log("Error: ", error);
    } else {
        console.log("Success: ", data.Buckets);
    }
});

//This is where my program creates the HTML file.
//It concatenates various strings into one large string creating the entirety of the HTML file in one string variable called "data".

s3.upload({
    Bucket: 'reporting',
    Key: s3Folder + platformPath + '/' + browserPath + '/' + title + '.html',
    Body: data
}, function (err, data) {
    if (err) {
        console.log("Error: ", err);
    } if (data) {
        console.log("Success: ", data.Location);
    }
});

//When run, the program seems to not even enter the 's3.upload()' function.
//It doesn't return an error or success message.

0 个答案:

没有答案
相关问题