-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3-upload.ps1
170 lines (146 loc) · 5.71 KB
/
s3-upload.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# ============================================================
# Will upload local source folder to the target S3 bucket.
# Please refer to script documentation for ways to configure.
# ============================================================
# This script REQUIRES AWS Tools for Windows PowerShell
# installed on default location, or specify Import-Module.
# Official download link: https://aws.amazon.com/powershell/
# ============================================================
# This is main part of the script, not really much to do here.
# To configure it, edit the configuration file: s3-upload.conf
# ============================================================
Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"
# SPECIFY PATH TO CONFIG FILE:
$configfile="C:\Scripts\s3-upload.conf"
# TYPES HOW WE RETRIEVE PRIVATE IP OF THE SERVER, ESPECIALLY ON AWS INSTANCES:
# http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/using-instance-addressing.html
#$iphtml = Invoke-WebRequest -Uri "http://169.254.169.254/latest/meta-data/local-ipv4"
#$privateip = $iphtml.Content
#$privateip = "172.16.1.2"
#$ipaddress = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .
$namahost = hostname
# LOAD CONFIGS FROM CONFIG FILE:
function ConvertFrom-Json20([object] $item){
add-type -assembly system.web.extensions
$ps_js=new-object system.web.script.serialization.javascriptSerializer
return ,$ps_js.DeserializeObject($item)
}
$content = ConvertFrom-Json20(Get-Content -Path $configfile -Delimiter "`0")
$content = $content[0]
$SMTPServer = $content | Select-Object -Property mail.host
$SMTPServer=$content.'mail.host'
$SMTPSecurity = $content.'mail.security'
$SMTPPort = $content | Select-Object -Property mail.port
$SMTPPort=$content.'mail.port'
$username = $content | Select-Object -Property mail.username
$username=$content.'mail.username'
$password = $content | Select-Object -Property mail.password
$password=$content.'mail.password'
$from = $content | Select-Object -Property mail.from
$from=$content.'mail.from'
$tolist = $content | Select-Object -Property mail.to
$tolist=$content.'mail.to'
$tolist = $tolist.Split(",")
# INITIALISE MAIL OBJECT:
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
if($SMTPSecurity.contains('none')){
$SMTPClient.EnableSsl = $false
}else{
$SMTPClient.EnableSsl = $true
}
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($username, $password);
$objects=$content | Select-Object -Property objects
$objects=$content.'objects'
foreach ($object in $objects){
# INITIALISE OBJECTS:
$location = $object.Get_Item('file.location')
$age = $object.Get_Item('file.age')
$bucket = $object.Get_Item('aws.bucket')
$accesskey = $object.Get_Item('aws.accesskey')
$secretkey = $object.Get_Item('aws.secretkey')
$region = $object.Get_Item('aws.region')
$awsfolder = $object.Get_Item('aws.folder')
$isdelete = $object.Get_Item('file.delete')
$iszip = $object.Get_Item('file.zip')
# FIND FILES USING THE GIVEN FILTER:
if($age.contains('M')){
$age = $age -replace "M", ""
$files = Get-ChildItem $location -Filter $object.'file.match' | Where{$_.LastWriteTime -gt (Get-Date).AddMonths(-$age)}
}
elseif($age.contains('d')){
$age = $age -replace "d", ""
$files = Get-ChildItem $location -Filter $object.'file.match' | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-$age)}
}elseif($age.contains('m')){
$age = $age -replace "m", ""
$files = Get-ChildItem $location -Filter $object.'file.match' | Where{$_.LastWriteTime -gt (Get-Date).AddMinutes(-$age)}
}elseif($age.contains('h')){
$age = $age -replace "h", ""
$files = Get-ChildItem $location -Filter $object.'file.match' | Where{$_.LastWriteTime -gt (Get-Date).AddHours(-$age)}
}
#"[DEBUG] Files after the filters are applied "+$files
# ITERATE OVER ALL THE FILES FOUND AFTER FILTERS ARE APPLIED:
foreach ($file in $files){
#"[DEBUG] Processing file "+$file
if($file){
Try{
$fullname = $file.FullName
$name = $file.Name
# UPLOAD FILE TO AWS S3:
$fullname
if($iszip){
$zipfilename = $name+".zip"
$zipfile = $fullname+".zip"
# INITIALIZE THE ZIP FILE:
if(-not (test-path($zipFile))) {
set-content $zipFile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipFile).IsReadOnly = $false
}
# CREATE ZIP PACKAGE:
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipFile)
$zipPackage.CopyHere($fullname)
# THIS 'WHILE' LOOP CHECKS EACH FILE IS ADDED BEFORE CONTINUING:
while($zipPackage.Items().Item($name) -eq $null){
Start-Sleep -milliseconds 500
}
#"[DEBUG] Uploading compressed file to S3: "+$zipfile
Write-S3Object -BucketName $bucket -Key $awsfolder$zipfilename -File $zipfile -AccessKey $accesskey -SecretKey $secretkey -Region $region
}else{
#"[DEBUG] Uploading uncompressed file to S3: "+$fullname
# IF EXCEPTION ON THE BELOW LINE:
Write-S3Object -BucketName $bucket -Key $awsfolder$name -File $fullname -AccessKey $accesskey -SecretKey $secretkey -Region $region
# CODE STOPS ON THE ABOVE LINE, IF EXCEPTION OCCURS, CODE EXITS ON THE ABOVE LINE, SO THE FILE WILL NOT BE DELETED.
}
if($isdelete){
Remove-Item $fullname -Recurse
}
}
# CATCH IF ANY ERRORS ARE OCCURRED:
Catch{
# FINDS THE ERROR:
$line = $_.InvocationInfo.ScriptLineNumber
$ErrorMessage = $_.Exception.Message
$ErrorMessage
$line
if($ErrorMessage.Contains("Cannot bind argument to parameter")){
''
}else{
foreach($to in $tolist){
$Subject = "Exception in uploading file on "+$namahost
$Body = "Error while uploading file: "+$fullname+". "+$ErrorMessage+" Check at line "+$line
# SENDMAIL:
$SMTPClient.Send($from, $to, $Subject, $Body)
}
}
}
}
# REMOVE THE TEMPORARY ZIP FILE THAT IS CREATED:
if($file){
if($iszip){
if(Test-Path $zipfile){
Remove-Item $zipfile -Recurse
}
}
}
}
}