简体   繁体   English

是否可以获取可读流中文件的信息?

[英]Is it possible to get infos of a file in readable stream ?

I am developing a sftp server with nodeJs and I need to upload a file. 我正在使用nodeJs开发sftp服务器,我需要上传文件。 I am using this module : node-sftp-server 我正在使用此模块: node-sftp-server

I got the event "writefile" there : 我在那里有事件“ writefile”:

session.on("writefile", function (_path, readstream) {

        console.warn("Receipt file " + Path.basename(_path));
        var uploadParams = { Bucket: Vertigo.getenv('APP_S3_BUCKETNAME'), Key: Path.basename(_path), Body: readstream }
        s3.upload(uploadParams, function (err, data) {
            if (err) {
                console.warn(err, err.stack)
                return err;
            } else {
                console.log("File: " + data.Key + " successfully uploaded");
                /**
                 * AJOUTER FICHIER DANS MONGODB
                 */
                receiptFile.insert({
                    date: new Date(Date.now()),
                    userId: ObjectID(request.params.userId),
                    file: {
                        s3BucketName: data.Bucket,
                        s3ObjectName: data.Key,
                        name: data.Key,
                        //size: data.Contents.Size, //Comment récuperer la taille ?
                        // mimetype: file.mimetypes //Comment récuperer le mimeType?
                    }
                }, function (err, result) {
                    if (err) {
                        var deleteParams = { Bucket: Vertigo.getenv('APP_S3_BUCKETNAME'), Key: Path.basename(_path) }
                        //Si une erreur s'est produite, on supprime les fichiers insérés précèdemment pour que la bdd et minio soient toujours synchronisés
                        s3.deleteObject(deleteParams, function (err, data) {
                            if (err) {
                                console.warn(err, err.stack)
                            }
                            console.log(deleteParams.Key + " : Deleted from : " + deleteParams.Bucket)
                        })
                        return err;
                    }
                    else {
                        console.log("Mongodb : File inserted in our database");
                    }
                })
            }
        });
        readstream.on("end", function () {
            console.warn("File has been wrote");
        });
    });
});
});

When I put the file on sftp console, I got the _path which is the name of file and a readable stream. 当我将文件放在sftp控制台上时,我得到了_path,它是文件名和可读流。 I need to do that in two step. 我需要分两个步骤进行操作。 Step 1 : I must upload the file on s3 SDK client (It actually works) Step 2 : I must register infos in mongoDB 步骤1:我必须在s3 SDK客户端上上传文件(它确实有效)步骤2:我必须在mongoDB中注册信息

But I need to know size and mimeType of the file to register it.. 但是我需要知道文件的大小和mimeType才能注册它。

The s3.upload method only return the BucketName, the filename, and the location of the file in the Bucket. s3.upload方法仅返回BucketName,文件名以及文件在Bucket中的位置。

How could I get the size and mimeType of the uploaded file ?? 如何获取上传文件的大小和mimeType?

If someone could help me, I would be grateful :) 如果有人可以帮助我,我将不胜感激:)

Try this : 尝试这个 :

s3.headObject({ Key: key, Bucket: bucket })
  .promise()
  .then(res => {
    const size = res.ContentLength;
    const type = res.ContentType;
  });

Look : http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#headObject-property 外观: http : //docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#headObject-property

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM