Closed beatrixcendana closed 3 years ago
Week 3 Step 4 ⬤⬤⬤⬤◯◯◯◯◯ | 🕐 Estimated completion: 10-20 minutes
This week, you will be going through steps to create a Timer Trigger Function delete all files in your Blob storage container every 5 minutes. This allows for greater security for your users.
:bulb: Tip: Cron expressions might help you with the last task.
To test your work, use Postman to send a POST request with an image to your previous HTTP trigger function that will save your file within your blob. Recall that a successful save of your file will cause you to see a response similar to the below:
{
"body" : "File Saved"
}
You should also double check that your file has been saved by navigating to your storage blobs.
Now run your timer trigger function using node index.js
and re-check your storage blobs - they should be gone. Your logs notifying you of the blobs' deletions should also be displayed in your trigger function's console.
💡 Yay! This means your timer trigger function worked.
First, we'll need to create a new Azure Function that runs on a schedule with the Timer Trigger template.
bunnimage-timer
and select Timer trigger
Schedule
as 0 */5 * * * *
to have the function be triggered every 5 minutes.@azure/storage-blob
packageTo delete all the blobs in the container, you can first list all the blobs, then loop through deleting each one as you go.
I tell Timer Trigger to run every day at 6:23 AM.
Week 3 Step 5 ⬤⬤⬤⬤⬤◯◯◯◯ | 🕐 Estimated completion: 10-20 minutes
This week, you will be going through steps to create an HTTP Trigger that takes parameter inputs and returns a download link for the given image.
bunnimage-download
that takes in "username" from the header
username
from blob storage using node-fetch
downloadUri
.BUNNIMAGE_ENDPOINT2
bunnimage-download/index.js
on the bunnimage
branchTo test your work, use Postman to send a GET request with a "username" in the header. You should see a response similar to the below:
:bulb: The username value should be the name of an image you have already uploaded to the blob storage. Do it fast, or your timer trigger will delete it!
{
"body" : {
"downloadUri" : "https://<YOUR_BLOB_STORAGE_URL>.blob.core.windows.net/images/<USERNAME>.png",
"success": true
}
}
💡 Yay! This means you successfully fetched a download link to your image.
In our module.exports
function, we'll want to:
Use the node-fetch
package we installed earlier this week to determine if the requested image is available.
Use if-else statements to determine the success of the retrieval. Remember to test for both jpeg and png extentions!
Send back a JSON object containing the download link.
Go ahead and merge this branch to main
to move on. Great work finishing this section!
⚠️ If you receive a
Conflicts
error, simply pressResolve conflicts
and you should be good to merge!
Week 3 Step 3 ⬤⬤⬤◯◯◯◯◯◯ | 🕐 Estimated completion: 10-20 minutes
Codename Blob + 3RR0R Handling
This week, you will be going through steps to handle POST requests with no data.
✅ Task:
codename
and uses it to name the image.uploadFile()
functionbunnimage/index.js
to thebunnimage
branch🚧 Test Your Work
To test your work, use Postman to send a POST request without an image attached. You should see a response similar to the below:
1: Modify the Function
Handle Empty Data
Now we'll want to handle cases where the POST request's body happens to be empty. In your original
module.exports
function, before you parse the body, make sure it exists! Only then should you upload your file to blob storage.:question: How do I catch empty POST requests?
Use an try-catch statement to catch when `parse-multipart` is unable to parse the empty body. If catches an error, set the `responseMessage` to "Sorry! No image attached." Otherwise, you can safely parse the body! ```js var responseMessage = "" try { var password = // get the header called "codename" // use parse-multipart to parse the body // determine the file-type here! responseMessage = await uploadFile((place your parsedBody here), (place the extension here), (place the "codename" here)); // fill the parameters in! } catch(err) { context.log("Undefined body image"); responseMessage = "Sorry! No image attached." } ``` > :bulb: Hint: `responseMessage` is what we're returning to the user as the output.