简体   繁体   中英

Create a new folder in s3 bucket with powershell

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

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 S3 object, so it's implied from the filename.

To indicate a specific key including a date prefix, you would 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"

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'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM