how can we upload multiple files in aws s3 bucket at the same time in specific folder location
Loading
how can we upload multiple files in aws s3 bucket at the same time in specific folder location
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
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Oct 26, 2023, 3:13 PM EDT
To upload multiple files to an AWS S3 bucket and get the URLs of the uploaded files in C#, you can use the AWS SDK for .NET. Below is an example of how to do this:
using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; namespace S3UploadExample { class Program { private const string bucketName = "YOUR_BUCKET_NAME"; private static readonly RegionEndpoint bucketRegion = RegionEndpoint.YOUR_REGION; private static IAmazonS3 s3Client; static async Task Main(string[] args) { s3Client = new AmazonS3Client(bucketRegion); List filePaths = new List { "FILE_PATH_1", "FILE_PATH_2", // Add more file paths as needed }; foreach (var filePath in filePaths) { await UploadFileAsync(filePath); } } private static async Task UploadFileAsync(string filePath) { try { var fileTransferUtility = new TransferUtility(s3Client); var keyName = "YOUR_PREFIX/" + Guid.NewGuid() + System.IO.Path.GetExtension(filePath); var fileTransferUtilityRequest = new TransferUtilityUploadRequest { BucketName = bucketName, FilePath = filePath, Key = keyName, CannedACL = S3CannedACL.PublicRead }; await fileTransferUtility.UploadAsync(fileTransferUtilityRequest); var url = GeneratePreSignedURL(bucketName, keyName); Console.WriteLine($"File upload successful. URL: {url}"); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message); } catch (Exception e) { Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); } } private static string GeneratePreSignedURL(string bucketName, string objectKey) { var request = new GetPreSignedUrlRequest { BucketName = bucketName, Key = objectKey, Expires = DateTime.Now.AddHours(1) }; string url = s3Client.GetPreSignedURL(request); return url; } } }