Is it possible to create a new folder, whose name is the current date, in an S3 bucket?
I am writing a script which, each time it executes, sends a file to an S3 bucket. I need it to make a new folder whose name is today's date.
Edit
I am using
Write-S3Object -BucketName $bucketName -File "$Directory\$file"
$file comes from a foreach cycle, it is a file within a local folder. $Directory is the path where the files are located
CodePudding user response:
You don't actually create folders in S3, though they may be shown like that when you use the web console and other viewers to make it easier to be readable by humans.
What you can do is add the date as the prefix to your file name when uploading it, so the destination file's name (or rather, the object's key) would be something like 'date/sourcename'.
CodePudding user response:
The first thing to be aware of is that you can simply upload a file named, for example, cat.png
to s3://mybucket/YYYY-MM-DD/cat.png
without having to pre-create YYYY-MM-DD/
in the bucket.
Your current script is:
Write-S3Object -BucketName $bucketName -File "$Directory\$file"
This doesn't explicitly set the key of the target S3 object, so it's implied from the filename.
To indicate a specific key including a date prefix, add the following when calling Write-S3Object:
-Key "2021-12-02/$file"
Of course, you might prefer to get today's date dynamically from PowerShell rather than hard-code 2012-12-02, and you can do that using:
Get-Date -Format "yyyy-MM-dd"