📺 Develpreneur YouTube Episode

Video + transcript

Python - AWS - Store and Retrieve Files

2023-10-24 •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. This episode uses the Boto3 library and AWS command line interface in a practical sense as we store and retrieve files to 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 our little uh backup project
we're looking at uh in particular doing
some Amazon AWS S3 stuff and uh dealing
with some of their buckets and storing
files
and also going to do some packaging of
files and things like that this time we
have um we've already set up stuff we've
we've got our S3 uh using B3 using that
Library we've got that set up got it
configured we created a bucket so now uh
and we've also been able to list a
bucket uh the buckets that we have so
now what we're going to do is we're
going to do some uh file manipulation so
what we're going to do is we're going to
add uh the ability to upload a file and
then also to bring one back down and
take a look at what's in there so what
we're going to do is uh we
have uh here we're going to do upload a
file to
S3 and it's actually very
straightforward uh because what we're
going to do is we're going to give it a
file which is basically whatever it is
to our local file and then we're going
to give it a bucket name and we're going
to load it up there so like we've done
before we're going to create a uh just
come in and get an instance of the Bodo
3 client and for the that's going to for
the S3 uh if you ever do and we'll
probably do some as I said some other
stuff where we'll get into some of the
other um AWS services and features
because there are definitely a lot of
things you can do with this uh as always
you know if you want to take a look at
what AWS offers look at aws.amazon.com
and just look at the I think they're up
to 150 200 Services something like that
lots of stuff out
there I dig grass so what we're going to
do is we're going to grab the file name
uh we're just going to do that based off
of the path here uh we're going to get
the the base name for the file whoops
and then all we have to do stop doing my
don't give me that uh basically we're
going to just tell it upload a file so
we're going to say where it is we're
going to tell it what the bucket name is
and then what is the file name that
we're going to save it as and then we're
going to do a little thing out here and
it's a straightforward exception so if
we look at uh let's just look here so
we're going to actually upload we're
going to upload this bucket project. py
and uh we call this upload file to whoop
to S3 so what we're going to do here is
if we give it upload then we're going to
give it the uh the file or the bucket
name in this case for upload uh let's do
it like this
way equals
4 let's see well yeah we'll leave it
like that
we're going to have two
usages so the first one is going to be
upload the second one's going to be
download let's make this a little less
confusing so for upload we're going to
do the name and then we're going to do
the Target and so the name is the file
name and then second is going to be the
target
bucket and let me make sure we've got
that right upload to S3 if we look at
that okay so the file and then the
bucket yeah so that's what we're going
to do so right now we're going to do an
upload with that so if we come
in let's see I save
it and it's going to start out
well let's comment these little helper
little things
out it's going to go with
that uh let's see oh this is going to
be
uh let's do
this if the length of arguments equals
three then it's going to be operation
name and Target operation name Target
upload there we go that's going to be
operation be one okay so there we should
be good so let's do
so if we do Python
3 uh what do we call this bucket
project and it's going to be
upload and we're going to upload bucket
project and we're going to upload it
into our little bucket that we created
which is this guy right
here so if we do
that uh operation is not defined uh
let's see interest interesting why did
it
say it did
that
uh did I miss something uh see one
two oh it is going to
be
four
that's what I made I'm sorry I
miscounted because for one of them is
one of the arguments is the
uh the name itself so that's the name of
the script and then we have three oh we
have two after that I'm sorry it's
python oh wait let's look at
this what did I just mess up I think I
just miscounted stuff so operation
equals equal name operation is not
Define oh because it wasn't equal to
three it's actually equal to four which
is correct right
1 two
3 one 2 3 4 there we go okay yes so
script action uh Source bucket so that's
four and so I need to do that
here
and actually I don't need that because
now I know it's equal to four okay sorry
I was overthinking that a little bit uh
so now if we run
it then uh we uploaded it to our uh up
to that bucket and so what we're going
to see is that uh let's
go we're going to list our buckets and
we're going to list bucket contents so
uh we actually not going to bother with
listing
buckets and we're just going to do it so
now we can run it without any let's do
this Python 3 bucket we're going to run
it and then we're going to
see uh right here oh I guess I I should
probably show you how we did this I
forgot so listing buckets is also very
straightforward we're going to come in
we're going to get a client as we've
done
before uh then we go like here it says
hello oh that's sorry that's list
buckets I want list bucket content which
is what we're calling down here and
we're just giving it a bucket so then we
come up to list bucket contents same
thing we get a we create a client uh we
grab the bucket and say hey this is the
bucket we're going to use and then for
each object in buckets we're just going
to print out here what's the file key
which happens to be the name and then
I'm throwing the object itself which is
basically a bucket name and a key right
now so we could do which we saw here
here's where we can just grab the key so
if we want the file name then we just
grab the key and we're off and running
if we wanted to make that more
complicated then we
could uh but here uh when we do our oops
that's downloaded when we upload we're
just using that file name so if we
wanted to do something more
complicated like let's say file name
plus let's call
it uh let's call it
archived if we do that and now we save
our
file
uh
then if we run it did I run oh I did
yeah so now if we run just this we're
going to see
that uh
here sorry it's like not the here let me
do it this way let me just quit because
then I'll make it a little easier let's
see if I do
this let's just quit after
this so we can see it and let's like
clean this up a little bit this so now
we're going to see that we have bucket
the file key was bucket project and then
bucket project
archived and so we just tacked
on uh here we just tacked on that string
so you can do whatever you want to set
that key uh typically it may be a file
name but depending on what you do it may
be something much more complicated than
that uh for example very often if it's a
file related to a uh like for an
application where you're going to upload
files for users then maybe it's going to
be like the user ID or something like
that followed by you know maybe you want
to do a dash and then um you the name of
the file or something like that or some
user you know entered name of the file
there are uh
some uh there are some restrictions a
little bit about how the names go but uh
you can always look those up on the site
generally speaking if you grab the file
name you should be safe so now what we
want to
do is uh we want to download a file so
we want to come in and we can
either here let's see we'll do it simply
this time what we're going to do is
we're going to download the bucket
contents uh
so this time we're just going to do uh a
name and a Target except for this is
going to be
download and the name is going to be the
bucket name correct make sure I got that
right yeah bucket name and then the
target is going to be the locate
directory now what I'm going to do here
we're not going to
get
uh we're going to keep this sort of
simple because what we're going to do is
we're going to come down again we create
our client uh grab our bucket and we're
going to create the local directory in
case we need to uh because we're going
to shove all these files into a location
and they're going to walk through each
object and then we are going to use the
local directory thing plus the key so
it's going to be plus the file name and
then we're going to try to download it
and then just download it in there so
it's really straightforward again it's
like download file and upload file S3 is
really easy to work with it's almost
like just you copying stuff around
anyway so now if I take uh
download and I'm going to download My
Bucket name where did my bucket name
go so I'm going take
that and I'm just going to put it in
Temp and let's see if it creates Temp
and throws those files on there uh so I
did oh that was that list I didn't
really need that any oh it's not going
to run it right now because I have that
and that so I don't need
that
um yeah I'm just going to leave it like
this for now we're keeping it really
simple in our
script um so now what we're going to see
is so I downloaded that I downloaded the
initial one and the archived one into
the temp and if we look at temp we're
going to see there there's the two files
now the interesting thing is you can see
where because we have the name different
size a little different but if we go
look
at uh we'll C it well let's look it over
here if we go look here in
temp look that looks very familiar
because that is the file that we
uploaded so I think we'll stop there for
this uh this time we're go in and uh
we've got some you know really simple
stuff we can go we can send files up to
S3 and we can bring them back down and
we'll just continue on looking at some
of the different features and utilities
that we want to use uh as always Sho us
an email at info@ developer.com if you
have any questions other than that go
out there and have yourself a great day
a great week and we will talk to you
next
[Music]
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 Mastermind
2024 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.92

well hello and welcome back we are

19.6

continuing our little uh backup project

22.519

we're looking at uh in particular doing

25.039

some Amazon AWS S3 stuff and uh dealing

28.439

with some of their buckets and storing

29.64

files

30.84

and also going to do some packaging of

32.719

files and things like that this time we

36.239

have um we've already set up stuff we've

38.6

we've got our S3 uh using B3 using that

42.68

Library we've got that set up got it

44.8

configured we created a bucket so now uh

49

and we've also been able to list a

50.16

bucket uh the buckets that we have so

52.359

now what we're going to do is we're

53.079

going to do some uh file manipulation so

55.68

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

57.239

add uh the ability to upload a file and

59.399

then also to bring one back down and

61.44

take a look at what's in there so what

63.399

we're going to do is uh we

66.36

have uh here we're going to do upload a

69.4

file to

72

S3 and it's actually very

74.4

straightforward uh because what we're

75.6

going to do is we're going to give it a

76.56

file which is basically whatever it is

78.24

to our local file and then we're going

80.119

to give it a bucket name and we're going

81.24

to load it up there so like we've done

83.64

before we're going to create a uh just

85.32

come in and get an instance of the Bodo

87.36

3 client and for the that's going to for

90.2

the S3 uh if you ever do and we'll

92.72

probably do some as I said some other

94.119

stuff where we'll get into some of the

96

other um AWS services and features

99.56

because there are definitely a lot of

100.96

things you can do with this uh as always

103.6

you know if you want to take a look at

104.759

what AWS offers look at aws.amazon.com

107.04

and just look at the I think they're up

109.439

to 150 200 Services something like that

111.799

lots of stuff out

113.24

there I dig grass so what we're going to

116.2

do is we're going to grab the file name

119.479

uh we're just going to do that based off

121.079

of the path here uh we're going to get

123.56

the the base name for the file whoops

125.799

and then all we have to do stop doing my

128.959

don't give me that uh basically we're

130.8

going to just tell it upload a file so

132.319

we're going to say where it is we're

134.04

going to tell it what the bucket name is

135.599

and then what is the file name that

137.12

we're going to save it as and then we're

139.319

going to do a little thing out here and

141.28

it's a straightforward exception so if

142.879

we look at uh let's just look here so

145.72

we're going to actually upload we're

147.16

going to upload this bucket project. py

151.4

and uh we call this upload file to whoop

153.68

to S3 so what we're going to do here is

157.8

if we give it upload then we're going to

160.68

give it the uh the file or the bucket

163.239

name in this case for upload uh let's do

166.44

it like this

169.36

way equals

175.12

4 let's see well yeah we'll leave it

179.319

like that

180.8

we're going to have two

182.959

usages so the first one is going to be

186.239

upload the second one's going to be

187.48

download let's make this a little less

190.519

confusing so for upload we're going to

193.72

do the name and then we're going to do

195.159

the Target and so the name is the file

201

name and then second is going to be the

204.72

target

206.48

bucket and let me make sure we've got

209.92

that right upload to S3 if we look at

213.4

that okay so the file and then the

215.599

bucket yeah so that's what we're going

217

to do so right now we're going to do an

218.28

upload with that so if we come

220.64

in let's see I save

223.04

it and it's going to start out

225.76

well let's comment these little helper

228.68

little things

230.84

out it's going to go with

235.36

that uh let's see oh this is going to

238.68

be

245.319

uh let's do

247.319

this if the length of arguments equals

256.959

three then it's going to be operation

260.28

name and Target operation name Target

263.639

upload there we go that's going to be

266.12

operation be one okay so there we should

267.88

be good so let's do

270.16

so if we do Python

272.12

3 uh what do we call this bucket

275.84

project and it's going to be

280.8

upload and we're going to upload bucket

283.52

project and we're going to upload it

285.84

into our little bucket that we created

287.96

which is this guy right

291.4

here so if we do

294.08

that uh operation is not defined uh

297.6

let's see interest interesting why did

300.44

it

305.6

say it did

308

that

313.52

uh did I miss something uh see one

318.56

two oh it is going to

322.8

be

328.479

four

330.759

that's what I made I'm sorry I

332.479

miscounted because for one of them is

334.44

one of the arguments is the

337.12

uh the name itself so that's the name of

340.08

the script and then we have three oh we

342.479

have two after that I'm sorry it's

343.759

python oh wait let's look at

346.319

this what did I just mess up I think I

348.639

just miscounted stuff so operation

350.6

equals equal name operation is not

354.44

Define oh because it wasn't equal to

356.759

three it's actually equal to four which

358.68

is correct right

360.12

1 two

361.8

3 one 2 3 4 there we go okay yes so

367.039

script action uh Source bucket so that's

372.24

four and so I need to do that

375.08

here

377.12

and actually I don't need that because

379.199

now I know it's equal to four okay sorry

381.56

I was overthinking that a little bit uh

383.479

so now if we run

385.52

it then uh we uploaded it to our uh up

390.12

to that bucket and so what we're going

392.199

to see is that uh let's

396.479

go we're going to list our buckets and

398.599

we're going to list bucket contents so

400.72

uh we actually not going to bother with

401.88

listing

402.88

buckets and we're just going to do it so

405.52

now we can run it without any let's do

409.68

this Python 3 bucket we're going to run

412.72

it and then we're going to

415.879

see uh right here oh I guess I I should

419.599

probably show you how we did this I

420.8

forgot so listing buckets is also very

423.479

straightforward we're going to come in

425.12

we're going to get a client as we've

426.4

done

427.28

before uh then we go like here it says

430.36

hello oh that's sorry that's list

432.4

buckets I want list bucket content which

435.479

is what we're calling down here and

437.96

we're just giving it a bucket so then we

439.639

come up to list bucket contents same

442.199

thing we get a we create a client uh we

444.879

grab the bucket and say hey this is the

446.639

bucket we're going to use and then for

448

each object in buckets we're just going

450

to print out here what's the file key

452.639

which happens to be the name and then

454.44

I'm throwing the object itself which is

456.319

basically a bucket name and a key right

459.08

now so we could do which we saw here

461.68

here's where we can just grab the key so

463.039

if we want the file name then we just

465.479

grab the key and we're off and running

467.68

if we wanted to make that more

468.879

complicated then we

470.879

could uh but here uh when we do our oops

475.599

that's downloaded when we upload we're

478.039

just using that file name so if we

479.879

wanted to do something more

481.96

complicated like let's say file name

485.8

plus let's call

490.599

it uh let's call it

493.199

archived if we do that and now we save

497.479

our

498.8

file

501.08

uh

502.919

then if we run it did I run oh I did

506.599

yeah so now if we run just this we're

509.039

going to see

510.68

that uh

513.76

here sorry it's like not the here let me

516.719

do it this way let me just quit because

519.039

then I'll make it a little easier let's

520.76

see if I do

523.08

this let's just quit after

525.92

this so we can see it and let's like

528.8

clean this up a little bit this so now

532

we're going to see that we have bucket

534.92

the file key was bucket project and then

536.76

bucket project

538.2

archived and so we just tacked

541.64

on uh here we just tacked on that string

544.079

so you can do whatever you want to set

545.64

that key uh typically it may be a file

547.959

name but depending on what you do it may

549.32

be something much more complicated than

551.079

that uh for example very often if it's a

553.959

file related to a uh like for an

556.519

application where you're going to upload

558.04

files for users then maybe it's going to

560.079

be like the user ID or something like

562.079

that followed by you know maybe you want

564.56

to do a dash and then um you the name of

567.839

the file or something like that or some

570.519

user you know entered name of the file

574.48

there are uh

576.76

some uh there are some restrictions a

579.76

little bit about how the names go but uh

582.519

you can always look those up on the site

584.56

generally speaking if you grab the file

586.88

name you should be safe so now what we

589.2

want to

590.48

do is uh we want to download a file so

594.44

we want to come in and we can

597.44

either here let's see we'll do it simply

600.64

this time what we're going to do is

601.76

we're going to download the bucket

604.519

contents uh

606.32

so this time we're just going to do uh a

609.839

name and a Target except for this is

611.56

going to be

612.519

download and the name is going to be the

614.36

bucket name correct make sure I got that

617.72

right yeah bucket name and then the

619.92

target is going to be the locate

624.959

directory now what I'm going to do here

626.839

we're not going to

628.04

get

629.56

uh we're going to keep this sort of

631.079

simple because what we're going to do is

633.32

we're going to come down again we create

636.2

our client uh grab our bucket and we're

639.959

going to create the local directory in

641.24

case we need to uh because we're going

643.24

to shove all these files into a location

646.2

and they're going to walk through each

647.6

object and then we are going to use the

650.279

local directory thing plus the key so

652.639

it's going to be plus the file name and

654.519

then we're going to try to download it

656.2

and then just download it in there so

658.48

it's really straightforward again it's

660.16

like download file and upload file S3 is

664.04

really easy to work with it's almost

665.6

like just you copying stuff around

667.72

anyway so now if I take uh

672.6

download and I'm going to download My

675.16

Bucket name where did my bucket name

678.44

go so I'm going take

683.16

that and I'm just going to put it in

685.959

Temp and let's see if it creates Temp

688.12

and throws those files on there uh so I

691.2

did oh that was that list I didn't

693.48

really need that any oh it's not going

695.56

to run it right now because I have that

698.48

and that so I don't need

702.639

that

704.6

um yeah I'm just going to leave it like

706.519

this for now we're keeping it really

708.279

simple in our

709.88

script um so now what we're going to see

713.2

is so I downloaded that I downloaded the

716.639

initial one and the archived one into

718.72

the temp and if we look at temp we're

722.279

going to see there there's the two files

724.8

now the interesting thing is you can see

726.24

where because we have the name different

727.839

size a little different but if we go

729.519

look

730.6

at uh we'll C it well let's look it over

733.72

here if we go look here in

736.36

temp look that looks very familiar

739.32

because that is the file that we

741.639

uploaded so I think we'll stop there for

744.16

this uh this time we're go in and uh

746.8

we've got some you know really simple

749.639

stuff we can go we can send files up to

752.04

S3 and we can bring them back down and

754.56

we'll just continue on looking at some

755.76

of the different features and utilities

757.24

that we want to use uh as always Sho us

759.88

an email at info@ developer.com if you

761.72

have any questions other than that go

763.8

out there and have yourself a great day

765.16

a great week and we will talk to you

767.92

next

770.28

[Music]

777.839

time

785.199

well hello this is Rob from develop

786.839

andur building better developers and we

788.959

have a mastermind class that will be

790.72

starting or actually several Mastermind

792.519

classes starting in 2024 check us out at

795.32

technology Mastermind

797.24

2024 we've got all the information you

799.32

need including some pricing early bird

801.519

discounts up to 50% off but those are

804.16

things that you're going to have to act

805.76

soon there is an application process so

808.839

go ahead and sign up today thanks a lot