Hi
My files are in AWS S3 folder, and I want to merge 2 file into sngle file.
read file from S3 folder and merge into single file.
Hi
My files are in AWS S3 folder, and I want to merge 2 file into sngle file.
read file from S3 folder and merge into single file.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jithu ThomasRank #437 · 5.7K reputationPosted Mar 5, 2024, 12:03 AM EST
Install the AWS SDK for .NET: You can install the AWS SDK for .NET using NuGet Package Manager in Visual Studio.
Write C# code to download the files from S3 and merge them into a single file:
using System; using System.IO; using Amazon; using Amazon.S3; using Amazon.S3.Model; class Program { static async Task Main(string[] args) { string accessKeyId = "YourAccessKeyId"; string secretAccessKey = "YourSecretAccessKey"; string bucketName = "YourBucketName"; string fileKey1 = "path/to/file1.txt"; string fileKey2 = "path/to/file2.txt"; string outputFilePath = "path/to/mergedfile.txt"; var region = RegionEndpoint.USWest2; // Change this to your desired AWS region using (var client = new AmazonS3Client(accessKeyId, secretAccessKey, region)) { await MergeFilesAsync(client, bucketName, fileKey1, fileKey2, outputFilePath); } } static async Task MergeFilesAsync(IAmazonS3 client, string bucketName, string fileKey1, string fileKey2, string outputFilePath) { try { using (var response1 = await client.GetObjectAsync(bucketName, fileKey1)) using (var response2 = await client.GetObjectAsync(bucketName, fileKey2)) using (var outputStream = new FileStream(outputFilePath, FileMode.Create)) { // Merge the content of the two files into a single file await response1.ResponseStream.CopyToAsync(outputStream); await response2.ResponseStream.CopyToAsync(outputStream); } Console.WriteLine("Files merged successfully!"); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } }Make sure to replace placeholders such as
YourAccessKeyId,YourSecretAccessKey,YourBucketName,path/to/file1.txt,path/to/file2.txt, andpath/to/mergedfile.txtwith your actual AWS credentials, bucket information, and file paths.Additionally, ensure that your AWS credentials have the necessary permissions to read from the specified S3 bucket and that your application has the required permissions for file operations.