Session | Basic JavaScript S3 SDK Exercises

Code in text editor

Basic JavaScript S3 SDK Exercises

In this section, you'll use the NodeJS AWS SDK to interact with the S3 service.

By default, AWS will use the configuration that is located in ~/.aws/configuration. This file contains the Access Key ID and Secret Access Key that you set when you typed $ aws configure.


Setup

  1. Make a new directory s3nodeexercises.
  2. Create a new file called s3repl.js.
  3. From the command line, type npm init. You can either set the values you want or hit enter to use the defaults.
  4. Install the aws-sdk package: npm install aws-sdk
  5. At the top of your s3repl.js, add the following code which will require the 'aws-sdk' and create an object you will use to interact with S3:
  6. var AWS = require('aws-sdk');
    s3 = new AWS.S3();

Remember, you don't need to set env variables or import keys since the SDK looks in your ~/.aws/configuration file.


Creating Buckets

Use the createBucket documentation (and/or Google) to write code that creates an empty bucket in your account.

Extensions:

  • Can you accept a dynamic value for the bucket name that is entered while the program is running?
  • Here's a hint: after npm install readline-sync, you can do something like this:
    var readline = require('readline-sync');
    var response = readline.question("<INSERT PROMPT HERE>");
  • Can you create a bucket where encryption is enabled automatically?
  • Can you create a bucket where versioning is enabled automatically?
  • Right now, your bucket is created in the default region you set during $ aws configure. Using code, can you change the region where your bucket is created?

Uploading Objects

Use the putObject documentation (and/or Google) to write code that uploads a file (object) in the bucket you just created.

Extensions:

  • Can you accept a dynamic value for the object to be uploaded that is entered while the program is running?

Listing Objects

Use the listObjects documentation (and/or Google) to write code that lists all of the objects in a bucket.

Extensions:

  • Can you accept a dynamic value for the bucket name that is entered while the program is running? Hint:
  • Can you accept a dynamic value for how many objects within a bucket should be returned that is entered while the program is running?

Deleting Objects

Use the deleteObject documentation (and/or Google) to write code that deletes an object in the bucket.

Extensions:

  • Can you accept a dynamic value for the object that is entered while the program is running?

Listing Buckets

Use the listBuckets documentation (and/or Google) to write code that lists all of the buckets in your account.

Extensions:


Full REPL Functionality

Use the code that you wrote in the above exercises to create a full REPL that has a similar interaction pattern to this:

What do you want to do? Enter one of the following:
b: list buckets
o: list objects in specific bucket
mb: make bucket
u: upload file
d: delete file
q: quit
------------
b
------------
Here are your buckets:
- myfirstwebsite
- images
What do you want to do? Enter one of the following:
b: list buckets
o: list objects in specific bucket
mb: make bucket
u: upload file
d: delete file
q: quit
------------
mb
------------
What would you like to call your bucket?
------------
documents
------------
Here are your buckets:
- myfirstwebsite
- images
- documents
What do you want to do? Enter one of the following:
b: list buckets
o: list objects in specific bucket
mb: make bucket
u: upload file
d: delete file
q: quit
------------
o
------------
For which bucket would you like to list your objects?
------------
images
------------
The objects in your images bucket are:
- penguin.jpg
- logo.png
- banner.jpg
What do you want to do? Enter one of the following:
b: list buckets
o: list objects in specific bucket
mb: make bucket
u: upload file
d: delete file
q: quit
------------
q
------------
Goodbye!