Naming Files
You can name your objects by using standard file naming conventions. You can use any valid name. If you’re planning on hosting a large number of files in your S3 bucket, there’s something you should keep in mind.
If all your file names have a deterministic prefix that gets repeated for every file, such as a timestamp format like YYYY-MM-DDThh:mm:ss
, then you will soon find that you’re running into performance issues when you’re trying to interact with your bucket.
00:00 Once you’re within your bucket, you can name your files however you’d like. We’re going to discuss a bit about the way S3 works, however, that might get you some better performance as your project grows.
00:11 As you add more and more files to an S3 bucket, the prefix of your files can become important. If they’re all related in some way, like the date and time, S3 can look at this prefix and assume that all of these files are related in some way.
00:27 What it will then do is group all these files together on the same disk partition. This can cause that partition to slow down if a lot of these resources are being accessed at the same time. To avoid this, instead of setting a standard prefix, you can randomize the filename.
00:44
Like with the bucket names before, we’ll take advantage of the uuid
module. And also like before, we’ll make a little helper function to do this as we need it. So, in your text editor, go ahead and define a new function called create_temp_file()
.
01:01
You’re going to pass in a size
, a file_name
, and some file_content
. This function is going to allow us to make a file with a random filename, and of a defined size that we tell it.
01:16
Go ahead and make a random_file_name
,
01:24
and what you’re going to do is join. And this time, the UUID goes first, so str(uuid.uuid(4))
, we’ll convert that to hex, and we really only need the first 6
characters of that. After this, just pass in the file_name
. Cool!
01:46
Now create a file by saying with open()
, pass in that random_file_name
, and you’re going to be writing a file.
01:55
Call this f
and then say f.write()
, and then just pass in that file_content
and multiply this by size
.
02:08
And then so we know what the name of that file is, just say return random_file_name
. All right. I’m going to go ahead and open up a terminal, get an interpreter going. Actually, let me bring this up.
02:25
And from boto3_guide import create_temp_file
.
02:37
And then just say something like first_file_name
and set this equal to create_temp_file()
. I want to set this to 300
, call this something like 'firstfile.txt'
, and then make that content just, like, the character 'f'
.
02:58
Cool. If you want to see what that first_file_name
is—
03:03 and there you go! In my case, it’s these six hexadecimal characters. And I’ve actually saved that file, so if I open that, you’ll just see there’s 300 f’s right there. All right, good stuff. And like before, I’m just going to grab this, copy it, and I’ll save it with my bucket names just so we have it for later. Okay.
03:29 Now that you can make files that have randomized prefixes, you’re ready to start uploading them to your S3 bucket. We’ll take a look at that in the next video. Thanks for watching.
Become a Member to join the conversation.
zulfiiaditto on April 20, 2023
Hello :) This course is life saver, thank you so much! The supporting materials include the following filles: animal class.py and Spreadsheet columns printer.py, which I belive not really correspond with boto3 course. LOL! Do you mind to provide snippets of codes without your credentials?
Thank you :)