· 7 years ago · Apr 13, 2018, 02:58 AM
1function Send-S3Files {
2 <#
3 .SYNOPSIS
4 Send the files from a local path to AWS S3
5 .DESCRIPTION
6 The function will copy all files from a specific path to AWS S3
7 .EXAMPLE
8 Send-S3Files -BucketName 'backups' -Region 'sa-east-1' -AKey '####' -SKey '####' -LocalSource 'c:\temp' , 'd:\backups'
9 #>
10 [CmdletBinding()]
11 Param (
12 [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage='Name of the bucket in AWS')][string]$BucketName
13 , [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage='Region used in AWS')][string]$Region
14 , [Parameter(Mandatory=$True, HelpMessage='AWS access key')][string]$Akey
15 , [Parameter(Mandatory=$True, HelpMessage='AWS secret key')][string]$SKey
16 , [Parameter(Mandatory=$True, ValueFromPipeline=$False, HelpMessage='Local machine paths')][string[]]$LocalSource
17 )
18
19 process {
20
21 Initialize-AWSDefaultConfiguration -AccessKey $AKey -SecretKey $SKey -Region $region
22
23 foreach($source in $sources) {
24 Set-Location $source
25 $files = Get-ChildItem '*.*' | Select-Object -Property Name #get all files in the folder
26
27 try {
28 if(Test-S3Bucket -BucketName $bucket) {
29 foreach($file in $files) {
30 if(!(Get-S3Object -BucketName $bucket -Key $file.Name)) {
31 Write-Host "Copying file : $file "
32 Write-S3Object -BucketName $bucket -File $file.Name -Key $file.Name -CannedACLName private -region $region
33 }
34 }
35 } Else {
36 Write-Host "The bucket $bucket does not exist."
37 }
38 } catch {
39 Write-Host "Error uploading file $file"
40 $Error
41 }
42 }
43 }
44
45}