读写s3节点js

时间:2021-05-20 23:10:34

I have a problem but I don't know how to solve it. I reading a file from s3, then I save it to a folder(downloads). After that, I read the file that I saved in the folder (that I saved in downloads) and re-size it + save it in a different folder with a different name (resized). Then, I want to read again this file in order to upload it again in its the new size but when I try to do that it throws an error that there is no such file in this directory but when I check it in my folder it dose. My guess it's trying to read before the image is written in the new folder (resized) How can I fix it?

我有一个问题,但我不知道如何解决它。我从s3读取文件,然后将其保存到文件夹(下载)。之后,我读取了我保存在文件夹中的文件(我保存在下载中)并重新调整大小+将其保存在具有不同名称(调整大小)的其他文件夹中。然后,我想再次阅读这个文件,以便以新的大小再次上传它但当我尝试这样做时会抛出一个错误,在这个目录中没有这样的文件,但当我在我的文件夹中检查它时。我的猜测是在图像写入新文件夹之前尝试阅读(已调整大小)如何解决?

update

so accroding to commits this is what I did but it disen't print out anything in fs.readFileAsync. Why?

所以这是我所做的,但它不能打印出fs.readFileAsync中的任何内容。为什么?

var image = new ImageResize(__dirname +'/../downloads/'+ hash + '.png');
  image.smartResizeDown({
    width: 200,
    height: 200
  }).then(function () {
    image.stream(function (err, stdout, stderr) {
        var writeStream = fs.createWriteStream(__dirname +'/../resized/'+ hash + 'resize.png');
        stdout.pipe(writeStream);

}).then(function(){
  fs.readFileAsync('C:/Users/user/Desktop/cloud/new/resized/'+hash+'resize.png', function(error, buf){

    if(error)
      console.log(error);
    else
      console.log('yay');
});
});

});

1 个解决方案

#1


0  

var image = new ImageResize(__dirname +'/../downloads/'+ hash + '.png');

image.smartResizeDown({
    width: 200,
    height: 200
})
.then(function () {
    return new Promise(function(resolve, reject) {
        image.stream(function (err, stdout, stderr) {
            var writeStream = fs.createWriteStream(__dirname +'/../resized/'+ hash + 'resize.png');
            stdout.pipe(writeStream);

            resolve();
        })
    });
})
.then(function(){
    fs.readFileAsync('C:/Users/user/Desktop/cloud/new/resized/'+hash+'resize.png', function(error, buf) {
        if(error)
          console.log(error);
        else
          console.log('yay');
    });
    return null;
});

Assuming bluebird object is in Promise named variable.

假设bluebird对象在Promise中命名为变量。

#1


0  

var image = new ImageResize(__dirname +'/../downloads/'+ hash + '.png');

image.smartResizeDown({
    width: 200,
    height: 200
})
.then(function () {
    return new Promise(function(resolve, reject) {
        image.stream(function (err, stdout, stderr) {
            var writeStream = fs.createWriteStream(__dirname +'/../resized/'+ hash + 'resize.png');
            stdout.pipe(writeStream);

            resolve();
        })
    });
})
.then(function(){
    fs.readFileAsync('C:/Users/user/Desktop/cloud/new/resized/'+hash+'resize.png', function(error, buf) {
        if(error)
          console.log(error);
        else
          console.log('yay');
    });
    return null;
});

Assuming bluebird object is in Promise named variable.

假设bluebird对象在Promise中命名为变量。