Hello community!
In this scenerio i want to upload a file in aws s3 bucket in month wise folder i want to upload that file in month folder
Let say september so i want to upload all the september file data in september folder so i have done all the required step for this
So bot is downloading yesterday’s file daily
File name example is like this-31082023 this is file
Problem is how can i upload this yesterday’s file in Aug folder?because knw the september has started so how can i upload data in aug folder? Like 31082023 so when the month end will come how will the bot can upload the data
Hello @Priyesh_Shetty, you can do it like this:
- Get Yesterday’s Date:
- Use the
DateTime.Now.AddDays(-1)
function to get yesterday’s date. Store it in a variable, e.g., yesterdayDate
.
- Format Date to ‘ddMMyyyy’:
- Format
yesterdayDate
to the desired format ‘ddMMyyyy’. You can use the .ToString("ddMMyyyy")
method.
- Upload File to AWS S3:
- Use the AWS SDK for .NET (Amazon S3) to upload the file to your S3 bucket.
- When specifying the S3 key (object key), use the format “yyyy/MM/{formattedDate}/{fileName}”. This will create a hierarchy of year/month/day in your S3 bucket.
Here is a higher level explanation:
Assign
DateTime yesterdayDate = DateTime.Now.AddDays(-1)
string formattedDate = yesterdayDate.ToString(“ddMMyyyy”)
Assign
string awsAccessKey = “your_access_key”
string awsSecretKey = “your_secret_key”
string bucketName = “your_bucket_name”
string filePath = “path_to_your_local_file\31082023.txt” // Replace with the actual file path
string s3Key = $“yyyy/MM/{formattedDate}/{Path.GetFileName(filePath)}”
Invoke Code
Arguments: awsAccessKey, awsSecretKey, bucketName, filePath, s3Key
Code:
var s3Client = new AmazonS3Client(awsAccessKey, awsSecretKey, RegionEndpoint.YourRegion);
var request = new PutObjectRequest
{
BucketName = bucketName,
Key = s3Key,
FilePath = filePath
};
s3Client.PutObject(request)
Log Message
"File uploaded to S3 in the following location: " + s3Key
Cheers! 
@rodrigo.simao there is no key for aws it is kept open by user…Folder name are like this
Example- August 2023,July 2023
So in that month folder name i have to upload a zip file-31082023 named like this
So the problem where iam stucked is when the month gets over…And new month get started…so in this process bot actually downloads yesterdays date file from a website…so when new month started how can i upload that yesterday’s file that is 31082023 august file in august folder? This is only concern i have today is 1st september and bot is searching for today’s folder…and it is uploading 31082023 in september folder.
- Get Yesterday’s Date:
- Use the DateTime.Now.AddDays(-1) function to get yesterday’s date. Store it in a variable, e.g., yesterdayDate.
- Format Date to ‘ddMMyyyy’:
- Format yesterdayDate to the desired format ‘ddMMyyyy’. You can use the .ToString(“ddMMyyyy”) method.
- Extract Year and Month:
- Extract the year and month from the formatted date. You can use the DateTime functions to do this.
- Create the S3 Key:
- Create the S3 key (object key) by dynamically combining the year, month, and file name.
- Use the following format: $“{year}/{month}/{formattedDate}/{fileName}”.
- Upload File to AWS S3:
- Use the AWS SDK for .NET (Amazon S3) to upload the file to your S3 bucket using the dynamically created S3 key.
// Get Yesterday’s Date
DateTime yesterdayDate = DateTime.Now.AddDays(-1);
// Format Date to ‘ddMMyyyy’
string formattedDate = yesterdayDate.ToString(“ddMMyyyy”);
// Extract Year and Month
int year = yesterdayDate.Year;
int month = yesterdayDate.Month;
// File Name
string fileName = “31082023.zip”; // Replace with the actual file name
// Create the S3 Key
string s3Key = $“{year}/{month}/{formattedDate}/{fileName}”;
// AWS S3 Upload Code (as shown in previous response)
// Log Message
"File uploaded to S3 in the following location: " + s3Key
Cheers! 