s3.getObject().createReadStream() :如何捕捉错误? [英] s3.getObject().createReadStream() : How to catch the error?

查看:22
本文介绍了s3.getObject().createReadStream() :如何捕捉错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序来从 s3 获取 zip 文件,将其解压缩,然后将其上传到 S3.但是我发现了两个我无法捕捉的异常.

I am trying to write a program to get a zip file from s3, unzip it, then upload it to S3. But I found two exceptions that I can not catch.

1. StreamContentLengthMismatch:流内容长度不匹配.收到 980323883 的 5770104761 字节. 这种情况不规则发生.

1. StreamContentLengthMismatch: Stream content length mismatch. Received 980323883 of 5770104761 bytes. This occurs irregularly.

2. NoSuchKey:指定的键不存在.当我输入错误的键时会发生这种情况.

2. NoSuchKey: The specified key does not exist. This happens when I input the wrong key.

当这两个异常发生时,程序就崩溃了.

When these two exceptions occur, this program crashes.

我想正确捕捉和处理这两个异常.

I'd like to catch and handle these two exceptions correctly.

我想防止崩溃.

   const unzipUpload = () => {
        return new Promise((resolve, reject) => {
            let rStream = s3.getObject({Bucket: 'bucket', Key: 'hoge/hoge.zip'})
                .createReadStream()
                    .pipe(unzip.Parse())
                    .on('entry', function (entry) {
                        if(entry.path.match(/__MACOSX/) == null){

                            // pause
                            if(currentFileCount - uploadedFileCount > 10) rStream.pause()

                            currentFileCount += 1
                            var fileName = entry.path;
                            let up = entry.pipe(uploadFromStream(s3,fileName))

                            up.on('uploaded', e => {
                                uploadedFileCount += 1
                                console.log(currentFileCount, uploadedFileCount)

                                //resume
                                if(currentFileCount - uploadedFileCount <= 10) rStream.resume()

                                if(uploadedFileCount === allFileCount) resolve()
                                entry.autodrain()
                            }).on('error', e => {
                                reject()
                            })
                        }

                    }).on('error', e => {
                        console.log("unzip error")
                        reject()
                    }).on('finish', e => {
                        allFileCount = currentFileCount
                    })
            rStream.on('error', e=> {
                console.log(e)
                reject(e)
            })
        })
    }

    function uploadFromStream(s3,fileName) {
        var pass = new stream.PassThrough();

        var params = {Bucket: "bucket", Key: "hoge/unzip/" + fileName, Body: pass};
        let request = s3.upload(params, function(err, data) {
            if(err) pass.emit('error')
            if(!err) pass.emit('uploaded')
        })
        request.on('httpUploadProgress', progress => {
            console.log(progress)
        })

        return pass
    }

这是我解压时使用的库.https://github.com/mhr3/unzip-stream

This is the library I use when unzipping. https://github.com/mhr3/unzip-stream

帮帮我!!

推荐答案

如果您想捕获 createReadStream 引发的 NoSuchKey 错误,您有两个选择:

If you'd like to catch the NoSuchKey error thrown by createReadStream you have 2 options:

  1. 在读取之前检查密钥是否存在.
  2. 从流中捕获错误

第一:

s3.getObjectMetadata(key)
  .promise()
  .then(() => {
    // This will not throw error anymore
    s3.getObject().createReadStream();
  })
  .catch(error => {
    if (error.statusCode === 404) {
      // Catching NoSuchKey
    }
  });

在解析来自 getObjectMetadata 的响应和运行 createReadStream

The only case when you won't catch error if file was deleted in a split second, between parsing response from getObjectMetadata and running createReadStream

第二个:

s3.getObject().createReadStream().on('error', error => {
    // Catching NoSuchKey & StreamContentLengthMismatch
});

这是一种更通用的方法,可以捕获所有其他错误,例如网络问题.

This is a more generic approach and will catch all other errors, like network problems.

这篇关于s3.getObject().createReadStream() :如何捕捉错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆