We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Dynamic Path

Although directories are an illusion in S3, they're still useful due to the prefix filtering capabilities of the S3 API. There are a lot of common strategies for organizing objects in S3, but the most important rule is:

Organization matters.

Schema architecture matters in a SQL database, and prefix architecture matters in S3. We always want to group objects in a way that makes sense for our case, because often we'll want to operate on a group of objects at once.

For example, pretend you do the naive thing and upload all your images to the root of your bucket. What happens if...

  • you want to delete all the images for a specific user?
  • a feature changed and you need to resize all the images it uses?
  • you want to change the permissions of all the images associated with a specific organization?

If you don't have any prefixes (directories) to group objects, you might find yourself iterating over every object in the bucket to find the ones you care about. That's slow and expensive.

Assignment

Tubely's software architect has decided on the following prefix "schema" for our video uploads:

  • landscape (16:9 aspect ratio)
  • portrait (9:16 aspect ratio)
  • other (everything else)
# mac
brew install ffmpeg

# linux
sudo apt install ffmpeg
ffprobe -version
ffmpeg -version
ffprobe -v error -print_format json -show_streams PATH_TO_VIDEO

You should see a streams array containing information about the video. We care about the width and height fields of the first stream.

    • It should use Bun.spawn to run the same ffprobe command as above. In this case, the command is ffprobe and the arguments are -v, error, -select_streams, v:0, -show_entries, stream=width,height, -of, json, and the file path.
    • Configure Bun.spawn to send the results to stdout and stderr. In this project, stdout and stderr are Web ReadableStreams, so you can convert them to text like this:
      const stdoutText = await new Response(proc.stdout).text();
      const stderrText = await new Response(proc.stderr).text();
      
    • Check the exited result. If the value is different than 0, it is an error.
    • Parse the stdout of the command so that you can get the width and height fields.
    • I did a bit of math to determine the ratio, then returned one of three strings: landscape (16:9), portrait (9:16), or other.

Aspect ratios might be slightly off due to rounding errors. You can use a tolerance range (or just use Math.floor and call it a day).

      • Title: "Boots Vertical"
      • Description: "A vertical video of Boots"
      • Use the vertical video
      • Title: "Boots Horizontal"
      • Description: "A horizontal video of Boots"
      • Use the horizontal video (this one might take a bit to upload)

Run and submit the CLI tests.