📺 Develpreneur YouTube Episode

Video + transcript

Python - AWS - Zip and store folders

2023-10-26 •Youtube

Detailed Notes

We continue the series of tutorials as we build out a backup system that drops archive files into Amazon's S3 and related storage. We step back a bit to look at packaging files into a compressed zip file and then storing it in the cloud in S3.

You can learn more through our online classes at https://school.develpreneur.com and register for free. Registration will add you to our email list, and you will periodically receive coupons for courses and notifications of the latest releases.

Transcript Text
[Music]
well hello and welcome back we are
continuing working on our little uh
backup script type thing and uh this
episode we're going to give ourselves
something a little more useful for the
backup so what we're going to do is
we're going to create something that is
a uh to zip some stuff up so what we're
going to do is we're going to use uh sh
util which is basically shell utils this
is super simple uh because what we're
going to do is we're going to then do uh
let's do
this uh we're going to create a method
that is going to be uh
ZIP and
store and it is going to be a
directory and a bucket
name and what we're going to do is we're
going to make an archive out of the
directory um oh let's give it a backup
name let's do that uh backup
name
and we're going to create a zip and then
what we're going to do is we're
basically going to turn around and do a
uh what do we call that
save um oh
upload so this going be upload file to
S3 and that one
takes a path and a bucket so it's going
to be the backup
name and then
the
uh bucket
name so let's see how this works uh oops
and then zip and
store let's see we're going to have one
more of these and this one's going to be
uh we'll call this
archive and let's do
it it's going to be the local directory
and the bucket name if we take those
so if um we're going to create another
operation here it's going to be
archive it's going to be zip and
store and it is going to
be uh how do we do
that call zip in store or we call it
store yeah store directory backup bucket
okay so directory is
name Target is the bucket actually let's
do
this that's going to be the file
name we'll call that archive
name and for right now we're going to
default the bucket which is going to be
this and this will just save us dealing
with
that so we come in here and we're going
to do zip and store so now what we're
going to
do is let's
take uh we're going to take this folder
including its temp and we're going to
store it so we're going to
do uh what do we call this bucket
project
archive and local directory is going to
be let's just just do this
users we're going to do the fully path
one S3
backup and then I want the archive name
which will be uh test backup that and
we'll just call it that because I think
we'll do it I think it'll tack the zip
on we're going to find out if we do that
what is it going to say uh no such file
or directed test backup so if we look
here oh it does test backup.
zip did it zip it up uh it
did so we're going to get rid of
him and it needs to
be uh let's
see when it does the
[Music]
upload uh we do
zip where did I do that so if we do
store and zip what it needs to do then
is because we created a zip file file
based off of that backup name then it
needs to be backup name
plus. zip and so now if we run
it boom it's
uploaded and if we wanted to pull it
back down so let's
take uh let's do this uh we're going to
do the
download where did I have that I've got
the somewhere back in my history there
we go so we're going to do it into temp
two and we are going to download
everything into that so now if we
look and we go to Temp two we can see
there is our test backup do I have unzip
I
do
um and it's going to unload it unzip it
here and we can see now that we've got
wh that we've got
our uh
tempar we've got it with all of its
little stuff so we have
created a uh
backup uh let's go ah a and get some of
this stuff cleaned
up so we created ourselves a backup uh
for uh we go in we give it a folder it's
going to back that thing up and then
it's going to uh unzip those folders for
and also note from the backup that it
came at the um at that level so
sometimes if you do tar or something
like that you may run into this but so
here uh let's see that's
archive it started at that S3 backup
level when I unzipped it that's where it
was at so if I wanted to recreate it it
would be I'd have to create that folder
S3 backup and then I would unzip my
files into to it and then boom we are
off and running so we now have a a way
to back up our files and store those out
in S3 now one of the things we probably
want to do is we want to look at the
ability
to uh we have download we have upload we
also want to be able
to delete so we're going to
do delete file
S3 and it's going to be a file
name and a
bucket and let's take a look at an
example for that so what we're going to
do is doing our handy dandy pulling some
stuff out of the documentation yet again
uh what we're going to do is oops I want
to do this because I don't think I have
client error set up nope oh I do okay so
I can leave
that um so delete fall S3 we're going to
give it a key and a bucket name so that
means whoa come on down
here this is going to look very similar
to let's
say
upload so
here if he is
delete
and he's going to
be delete file S3 we're going to give it
a name and a
Target and again like most of these it's
just going to be delete object so we're
going to give it a bucket we're going to
give it a key which is the file name uh
remember that could be we're assuming
it's a file name in our case but it
could be something else so uh then we're
going to delete it so let's
go look at at our bucket contents
first and
uh uh let's get rid of test
backup and if we do let's just run it uh
so if we do Python 3
bucket then we've got so these are the
guys we've got so let's take this one
and we're going to get rid of him get
rid of him
first so what we want to do is our
action is
delete the name is that and the bucket
name is
this which again we could simplify but
we'll keep it we'll send that so now if
we do
delete
then uh did it tell us it didn't really
tell us anything useful oh because we
quit so let's get rid of that and
that and now run it and it's going to
say deleted object oh it didn't pull
that stuff around
properly uh why where's deleted deleted
deleted
deleted
let's just do
this and instead of that that's going to
be
key do a little bit of fixing here so
we'll just uh make sure that it brings
that in
right it's going to be bucket names we
don't need
those okay I don't want spotlights
search
that
and we'll just keep it here and this
will say could not
delete couldn't delete
object there we go okay so now if we
come back down here and we do our handy
dandy list bucket
contents we should not see archived
anymore and we do not let's like CL that
list up a little bit uh where is list
that's list buckets list bucket content
let's just do ah we'll do the we don't
need the object so we're going to just
do the file
key so if we look we see we do not have
it now let's get rid of test backup as
well uh so if we go to delete the only
thing we need to change
here
is test
backup.
zip and
oh because I'm still giving my
list let's actually run
it and now we deleted it from that
bucket and so now if we' List
It
then now we've only got our one file
left so we
have uh let's do
this we'll do that and that just gives
us something to work with so now we have
put together quite a few little
utilities we can delete files we can add
files we can zip stuff up so we're
getting pretty close to our little uh
our archiver type thing so we'll do a
little more work with that next time
around and uh be able to like put quite
a bit into this little script and now
suddenly you've got a nice little Backup
Tool that you can push stuff out to you
know get it off of your server or your
local machine and store it out somewhere
else where it's protected there are much
more things we can do with it but we're
going to just go with that for now I
give you some good little examples as
always if you have any questions give us
uh shoot us an email at info@ developer
and other than that go out there and
have yourself a great day great week and
we will talk to you next time well hello
this is Rob from develop andur building
better developers and we have a
mastermind class that will be starting
or actually several Mastermind classes
starting in 2024 check us out at
technology
mastermind2 we've got all the
information you need including some
pricing early bird discounts up to 50%
off but those are things that you're
going to have to act soon there is an
application process
so go ahead and sign up today thanks a
lot
Transcript Segments
0

[Music]

16.96

well hello and welcome back we are

18.96

continuing working on our little uh

20.439

backup script type thing and uh this

23.4

episode we're going to give ourselves

25.279

something a little more useful for the

26.76

backup so what we're going to do is

29.199

we're going to create something that is

30.92

a uh to zip some stuff up so what we're

33.68

going to do is we're going to use uh sh

36.04

util which is basically shell utils this

38.48

is super simple uh because what we're

41.039

going to do is we're going to then do uh

43.28

let's do

44.6

this uh we're going to create a method

47.52

that is going to be uh

50.719

ZIP and

52.92

store and it is going to be a

58.12

directory and a bucket

63

name and what we're going to do is we're

65.799

going to make an archive out of the

74.64

directory um oh let's give it a backup

77.799

name let's do that uh backup

88.84

name

91

and we're going to create a zip and then

93

what we're going to do is we're

94.439

basically going to turn around and do a

96.72

uh what do we call that

99.2

save um oh

103

upload so this going be upload file to

105.68

S3 and that one

108.2

takes a path and a bucket so it's going

112.2

to be the backup

116.719

name and then

118.84

the

120.56

uh bucket

123.759

name so let's see how this works uh oops

127.079

and then zip and

129.64

store let's see we're going to have one

131.84

more of these and this one's going to be

134.56

uh we'll call this

138.28

archive and let's do

144.04

it it's going to be the local directory

147.319

and the bucket name if we take those

153.2

so if um we're going to create another

156.879

operation here it's going to be

166.599

archive it's going to be zip and

170.68

store and it is going to

173.519

be uh how do we do

176.68

that call zip in store or we call it

179.08

store yeah store directory backup bucket

182.799

okay so directory is

186.56

name Target is the bucket actually let's

189.799

do

191.04

this that's going to be the file

198.04

name we'll call that archive

202.04

name and for right now we're going to

204.36

default the bucket which is going to be

207.56

this and this will just save us dealing

209.519

with

212

that so we come in here and we're going

215.48

to do zip and store so now what we're

216.799

going to

218.2

do is let's

221.12

take uh we're going to take this folder

225.36

including its temp and we're going to

227.239

store it so we're going to

230.319

do uh what do we call this bucket

234.239

project

236.159

archive and local directory is going to

239.2

be let's just just do this

241.2

users we're going to do the fully path

246.959

one S3

250.64

backup and then I want the archive name

253.68

which will be uh test backup that and

258.359

we'll just call it that because I think

259.799

we'll do it I think it'll tack the zip

261.28

on we're going to find out if we do that

263.32

what is it going to say uh no such file

265.639

or directed test backup so if we look

267.96

here oh it does test backup.

271.08

zip did it zip it up uh it

275.56

did so we're going to get rid of

278.039

him and it needs to

282.12

be uh let's

284.32

see when it does the

286.63

[Music]

288.44

upload uh we do

293.68

zip where did I do that so if we do

295.759

store and zip what it needs to do then

298

is because we created a zip file file

299.84

based off of that backup name then it

302.759

needs to be backup name

306.199

plus. zip and so now if we run

314.08

it boom it's

316.84

uploaded and if we wanted to pull it

319.479

back down so let's

323.08

take uh let's do this uh we're going to

325.639

do the

327.28

download where did I have that I've got

329.36

the somewhere back in my history there

330.8

we go so we're going to do it into temp

336.28

two and we are going to download

339.12

everything into that so now if we

341.96

look and we go to Temp two we can see

346

there is our test backup do I have unzip

349.44

I

350.56

do

354.84

um and it's going to unload it unzip it

358.479

here and we can see now that we've got

360.919

wh that we've got

363.199

our uh

365

tempar we've got it with all of its

367.16

little stuff so we have

370.639

created a uh

374.68

backup uh let's go ah a and get some of

376.599

this stuff cleaned

379.4

up so we created ourselves a backup uh

383.08

for uh we go in we give it a folder it's

385.759

going to back that thing up and then

387.639

it's going to uh unzip those folders for

390.24

and also note from the backup that it

393.16

came at the um at that level so

398.44

sometimes if you do tar or something

399.68

like that you may run into this but so

401.319

here uh let's see that's

403.96

archive it started at that S3 backup

408.56

level when I unzipped it that's where it

411.639

was at so if I wanted to recreate it it

414.24

would be I'd have to create that folder

415.84

S3 backup and then I would unzip my

418.8

files into to it and then boom we are

420.84

off and running so we now have a a way

423.639

to back up our files and store those out

426.599

in S3 now one of the things we probably

430.24

want to do is we want to look at the

432.879

ability

434.4

to uh we have download we have upload we

439.08

also want to be able

441.28

to delete so we're going to

446.879

do delete file

452.84

S3 and it's going to be a file

456.639

name and a

459.56

bucket and let's take a look at an

461.599

example for that so what we're going to

464.159

do is doing our handy dandy pulling some

466.4

stuff out of the documentation yet again

468.879

uh what we're going to do is oops I want

470.599

to do this because I don't think I have

472.44

client error set up nope oh I do okay so

475.24

I can leave

476.68

that um so delete fall S3 we're going to

480.199

give it a key and a bucket name so that

483.4

means whoa come on down

488.08

here this is going to look very similar

491.039

to let's

492.44

say

498.72

upload so

503.8

here if he is

508.24

delete

510.319

and he's going to

515.719

be delete file S3 we're going to give it

519.159

a name and a

522.12

Target and again like most of these it's

524.56

just going to be delete object so we're

526.08

going to give it a bucket we're going to

527.08

give it a key which is the file name uh

529.76

remember that could be we're assuming

531.56

it's a file name in our case but it

533

could be something else so uh then we're

535.279

going to delete it so let's

537.64

go look at at our bucket contents

545

first and

554.64

uh uh let's get rid of test

559.56

backup and if we do let's just run it uh

562.8

so if we do Python 3

567.64

bucket then we've got so these are the

570

guys we've got so let's take this one

571.959

and we're going to get rid of him get

574.079

rid of him

575.32

first so what we want to do is our

577.839

action is

578.92

delete the name is that and the bucket

583.32

name is

587

this which again we could simplify but

589.399

we'll keep it we'll send that so now if

591.88

we do

594.04

delete

596.48

then uh did it tell us it didn't really

598.839

tell us anything useful oh because we

602.32

quit so let's get rid of that and

607.279

that and now run it and it's going to

609.92

say deleted object oh it didn't pull

612.839

that stuff around

616.32

properly uh why where's deleted deleted

619.519

deleted

628.04

deleted

630.839

let's just do

635.04

this and instead of that that's going to

637.2

be

641.68

key do a little bit of fixing here so

644.36

we'll just uh make sure that it brings

647.04

that in

648.68

right it's going to be bucket names we

651.279

don't need

655.959

those okay I don't want spotlights

659.04

search

661.04

that

662.839

and we'll just keep it here and this

665.36

will say could not

667.44

delete couldn't delete

672.24

object there we go okay so now if we

676.6

come back down here and we do our handy

679.68

dandy list bucket

682.279

contents we should not see archived

685.36

anymore and we do not let's like CL that

689.32

list up a little bit uh where is list

691.48

that's list buckets list bucket content

694.2

let's just do ah we'll do the we don't

696.56

need the object so we're going to just

698.48

do the file

702.36

key so if we look we see we do not have

705.24

it now let's get rid of test backup as

707.56

well uh so if we go to delete the only

710.68

thing we need to change

714

here

716.76

is test

719.68

backup.

723.279

zip and

725.76

oh because I'm still giving my

733.959

list let's actually run

737.56

it and now we deleted it from that

740.639

bucket and so now if we' List

747.88

It

753.279

then now we've only got our one file

756.279

left so we

758.72

have uh let's do

768.639

this we'll do that and that just gives

772

us something to work with so now we have

774.32

put together quite a few little

776

utilities we can delete files we can add

777.92

files we can zip stuff up so we're

780.279

getting pretty close to our little uh

782.519

our archiver type thing so we'll do a

785.32

little more work with that next time

786.639

around and uh be able to like put quite

789.279

a bit into this little script and now

791.04

suddenly you've got a nice little Backup

792.519

Tool that you can push stuff out to you

795.44

know get it off of your server or your

797.16

local machine and store it out somewhere

798.8

else where it's protected there are much

800.72

more things we can do with it but we're

801.8

going to just go with that for now I

803.04

give you some good little examples as

805.079

always if you have any questions give us

806.6

uh shoot us an email at info@ developer

810.16

and other than that go out there and

811.12

have yourself a great day great week and

813.04

we will talk to you next time well hello

816.44

this is Rob from develop andur building

818.24

better developers and we have a

820.32

mastermind class that will be starting

821.92

or actually several Mastermind classes

823.72

starting in 2024 check us out at

826.16

technology

828.079

mastermind2 we've got all the

829.72

information you need including some

831.04

pricing early bird discounts up to 50%

833.839

off but those are things that you're

835.519

going to have to act soon there is an

837.839

application process

839.32

so go ahead and sign up today thanks a

841.92

lot