file - Code/Script to recursively search S3 bucket to unzip (GZIP) and move specific file from ZIP to new location
问题描述
Hi I have an S3 bucket containing gzip files. Within each zip there is a single TSV file I want to move to a new folder or bucket (dont really mind which). The S3 bucket will be added to with new zip file each hour so this script needs to be something I can schedule or trigger. Happy to use CLI, Lambda or any other method! Pointers, links, help very much appreciated.
解决方案
好的,所以这样做的软糖方法是使用本地处理:
连接到 S3
AmazonS3Config 配置 = 新 AmazonS3Config(); config.ServiceURL = " https://s3-eu-west-1.amazonaws.com "; AmazonS3Client s3Client = new AmazonS3Client(S3AccessKey, S3SecretKey, config);
复制您要处理的文件
S3DirectoryInfo dir = new S3DirectoryInfo(s3Client, bucketname, "jpbodenukproduction"); dir.CopyToLocal(@"C:\S3Local");
解压 Gzip(包含 tar,包含多个文件):
字符串目录路径 = @"C:\S3Local"; DirectoryInfo directoryselected = new DirectoryInfo(directorypath); foreach (FileInfo FileToDecompress in directoryselected.GetFiles("*.gz")) { Decompress(FileToDecompress);
}公共静态无效解压缩(FileInfo fileToDecompress){使用(FileStream originalFileStream = fileToDecompress.OpenRead()){ string currentFileName = fileToDecompress.FullName; 字符串 newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);
using (FileStream decompressedFileStream = File.Create(newFileName)) { using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress)) { decompressionStream.CopyTo(decompressedFileStream); Console.WriteLine("Decompressed: {0}", fileToDecompress.Name); } } } }
现在处理 tar 文件(使用 ICSharpCode.SharpZipLib):
foreach (FileInfo TarFile in directoryselected.GetFiles("*.tar")) { var stream = File.OpenRead(TarFile.FullName); var tarArchive = ICSharpCode.SharpZipLib.Tar.TarArchive.CreateInputTarArchive(stream); tb1.Text = "正在处理:" + TarFile.Name; 尝试 { tarArchive.ExtractContents(@"C:\S3Local\Trash\"); } catch (Exception ziperror) { tb1.Text = "TarUnzip 中的延迟错误:" + ziperror; 线程.睡眠(10000);} 最后 { tarArchive.Close(); 流。关闭();
}
最后对解压缩的文件做你想做的事,我只是提取了我需要的单个文件,重新压缩并移回 S3。
我的计划是接下来转换为 Lambda 并按计划运行。
推荐阅读
- ios - 当我尝试以编程方式添加 UIView 元素时,视频未在 UIView 上播放
- python - 如何从 unet_learner (fastai) 获得正确的输出预测?
- python - 如何制作一个自动选择并准备写入的 tkinter 输入字段?
- c# - 如何检查已发布的自包含应用程序的 .NET Core 版本?
- python-3.x - 对存储在变量中的多个关键字使用运行关键字
- cordova - Apache cordova Build 在实施 firebase 时失败:依赖失败
- ubuntu - rsyslog stop 命令复制消息而不是移动
- google-bigquery - 如何在 BigQuery 的 WITH 下的 select 中构建表?
- mysql - MySql 在 WHERE 中使用变量
- android - 是否可以重新路由 FCM 的应用内消息弹出窗口?