创建 zip 并将其上传到 s3 时遇到困难

问题描述 投票:0回答:1

在 AWS S3 存储桶中,有多个文件(数量未知)。我有一个要求,我的 node.js 代码将所有这些文件压缩到一个 zip 中,然后将其上传回同一个存储桶中。我使用“archiver”模块来获取单个对象,将其压缩并使用流将其上传回 s3。以下是压缩和上传程序的代码片段:

//s3Contents is the output of listV2Command 
public async zipAllFiles(s3Contents:_Object[]){
        try{
            const archive=archiver('zip');
            archive.on('error', (err)=>{
                console.log(`Encountered error while zipping: ${JSON.stringify(err)}`);
                throw(err);
            });


            const upload=new Upload({
                client:this.s3Client,
                params:{
                    Bucket:process.env.BUCKET_KEY,
                    Key:`${this.folderName}/${this.jobId}.zip`,
                    Body:Readable.from(archive)
                }
            });
            for(let i=0;i<s3Contents.length;i++){
                const getParams={
                    Bucket:process.env.BUCKET_KEY,
                    Key:s3Contents[i].Key
                };
                const objectData=await this.getObjectFromS3(getParams); //This uses a different pool of S3Clients
                
                if(objectData.Body!==undefined){
                    const splitFolderAndFiles=s3Contents[i].Key?.split("/");
                    if(splitFolderAndFiles===undefined)
                        return;

                    const renderedFileName=splitFolderAndFiles[splitFolderAndFiles?.length-1];
                    console.log(`Appending: ${renderedFileName}`);
                    archive.append(objectData.Body as unknown as Readable, { name: renderedFileName || '' });;
                }
            }
            archive.finalize();
            
            

            this.logToCloudWatch('Uploading!!');
            await upload.done();
            
            this.logToCloudWatch('Put the zip successfully into s3');
        }catch(err){
            console.log('Zipping error');
            this.logToCloudWatch(`Error while zipping: ${JSON.stringify(err)}`);
        }
    }

现在,当对象数量很少(大约 50-100)时,这将按预期工作,从 s3 读取,压缩它并将其上传回 s3 存储桶。但是当对象数量增加时(224 个对象,每个 1MB),它就会卡在等待 upload.done() 上。我也尝试过等待半个小时来解决,但没有解决。继续等待是没有意义的,因为即使解决了,周转时间也太长了,没有任何用处。老实说,我完全不理解 @aws-sdk/lib-storage 的上传。 如果有人能向我指出我的这种方法有什么问题,那么我将不胜感激。谢谢...

typescript amazon-web-services amazon-s3 aws-sdk
1个回答
0
投票

你找到解决办法了吗?我看到类似的东西。

© www.soinside.com 2019 - 2024. All rights reserved.