-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
163 lines (128 loc) · 5.5 KB
/
main.py
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
from tkinter import *
from tkinter import messagebox
import boto3
import webbrowser
import tkinter as tk
# Initialize
fields = ('AWS Access Key', 'AWS Secret Key','S3 Bucket (Format: bucketname)', 'Folder to Restore (Format: folder1/folder2/)', 'Bucket Region (Example: us-east-1)',)
# Enable Hyperlinking
def hyperlink(url):
webbrowser.open_new(url)
# Create an S3 Client Session
def create_client(entries):
client = boto3.client(
's3',
aws_access_key_id=entries['AWS Access Key'].get(),
aws_secret_access_key=entries['AWS Secret Key'].get(),
region_name=entries['Bucket Region (Example: us-east-1)'].get()
)
return client
def restore_glacier_contents(entries, type):
client = create_client(entries)
bucket = entries['S3 Bucket (Format: bucketname)'].get()
prefix = entries['Folder to Restore (Format: folder1/folder2/)'].get()
# Try to get S3 list
try:
s3_result = client.list_objects_v2(
Bucket=bucket,
Prefix=prefix
)
except:
messagebox.showerror("Error", "You are unable to list these files.")
exit()
if 'Contents' not in s3_result:
return []
file_list = []
# Get list of Contents
for file in s3_result['Contents']:
# Only Log Glacier Files
if file['StorageClass'] == 'GLACIER':
if type=="Bulk":
response = client.restore_object(Bucket=bucket, Key=file['Key'],RestoreRequest={'Days': 14, 'GlacierJobParameters': {
'Tier': 'Bulk'}})
elif type=="Standard":
response = client.restore_object(Bucket=bucket, Key=file['Key'],RestoreRequest={'Days': 14, 'GlacierJobParameters': {
'Tier': 'Standard'}})
file_list.append(file['Key'])
# Get list of Contents when More than 1000 Items
while s3_result['IsTruncated']:
continuation_key = s3_result['NextContinuationToken']
s3_result = s3_conn.list_objects_v2(Bucket=bucket, Prefix=prefix, Delimiter="/", ContinuationToken=continuation_key)
for file in s3_result['Contents']:
# Only Log Glacier Files
if file['StorageClass'] == 'GLACIER':
if type=="Bulk":
response = client.restore_object(Bucket=bucket, Key=file['Key'],RestoreRequest={'Days': 14, 'GlacierJobParameters': {
'Tier': 'Bulk'}})
elif type=="Standard":
response = client.restore_object(Bucket=bucket, Key=file['Key'],RestoreRequest={'Days': 14, 'GlacierJobParameters': {
'Tier': 'Standard'}})
file_list.append(file['Key'])
# Return File List
return file_list
def glacier_restore(entries, type):
if type=="Bulk":
files = restore_glacier_contents(entries, type)
#print(files)
# If Bulk Success
messagebox.showinfo("Bulk Request Success!", "Bulk Restore Request Successful! Your files will be restored in 5-12 hours.")
elif type=="Standard":
files = restore_glacier_contents(entries, type)
#print(files)
# If Standard Success
messagebox.showinfo("Standard Request Success!", "Standard Restore Request Successful! Your files will be restored in 3-5 hours.")
else:
# If Failure
messagebox.showerror("Error", "There was an issue requesting restore of files, please contact your S3 administrator.")
def makeform(root, fields):
# Initialize Entries
entries = {}
for field in fields:
row = Frame(root)
# Add Field Title
lab = Label(row, width=50, text=field+": ", anchor='w')
# Add Field Entry
if field=="AWS Secret Key":
# AWS Secret Key is sensitive data and should be obscured
ent = Entry(row, width=61, show="*")
else:
ent = Entry(row, width=61)
# Add Styling
row.pack(side = TOP, fill = X, padx = 5 , pady = 5)
lab.pack(side = LEFT)
ent.pack(side = LEFT, expand = YES, fill = X)
# Save to Entries
entries[field] = ent
return entries
if __name__ == '__main__':
# Init TK
root = Tk()
# Set Window Title
root.title("AWS Glacier Restore to S3")
lab1 = Label(root, text="AWS Glacier Restore to S3", font=('Helvetica', 14))
lab1.pack(side = TOP)
lab2 = Label(root, text="Restoration will take a few minutes as files restore 1 by 1, program is working even if it says it is Not Responding.", font=('Helvetica', 10))
lab2.pack(side = TOP)
# Add Hyperlink to AWS Region Documentation
link2 = Label(root, text="GitHub", fg="blue", cursor="hand2")
link2.pack(side = BOTTOM, fill = X, padx = 5 , pady = 5)
link2.bind("<Button-1>", lambda e: hyperlink("https://github.com/nathanielkam/python-glacier-restore"))
# Add Hyperlink to AWS Region Documentation
link1 = Label(root, text="Documentation: AWS Region Codes", fg="blue", cursor="hand2")
link1.pack(side = TOP, fill = X, padx = 5 , pady = 5)
link1.bind("<Button-1>", lambda e: hyperlink("https://docs.aws.amazon.com/general/latest/gr/rande.html"))
# Init the Form
ents = makeform(root, fields)
# Add Quit Button
b3 = Button(root, text = 'Quit', command = root.quit)
b3.pack(side = RIGHT, padx = 5, pady = 5)
# Add Bulk Retrieval Button
b1 = Button(root, text = 'Bulk Retrieval (5-12 Hours)',
command=(lambda e = ents: glacier_restore(e, "Bulk")))
b1.pack(side = RIGHT, padx = 5, pady = 5)
# Add Standard Retrieval Button
b2 = Button(root, text='Standard Retrieval (3-5 Hours)',
command=(lambda e = ents: glacier_restore(e, "Standard")))
b2.pack(side = RIGHT, padx = 5, pady = 5)
# Run
root.mainloop()