Closed mariiahnied closed 3 years ago
Week 3 Step 2 ⬤⬤◯◯◯◯◯◯◯ | 🕐 Estimated completion: 10-20 minutes
This week, you will be going through steps to upload images to blob storage using Azure's SDK.
test
+ the correct file extensionBUNNIMAGE_ENDPOINT
, add your blob url to your repository secrets with the name blob_url
, and commit your updated function code to bunnimage/index.js
on the bunnimage
branchTo test your work, you'll be using Postman to send a POST request in Postman with an image in the body to your function url. You should see a response similar to the below:
{
"body" : "File Saved"
}
💡 Yay! This means it was successfully saved.
parse-multipart
to receive an imageIn your main module.exports
function, you'll want to use the parse-multipart
library to parse the image from the POST request. Then you'll determine the fle extension, and then upload the file using an uploadFile()
function we'll write later on.
Let's start out by writing the function we can call to upload an iamge.
⬇ Uploading the image blob to your container
Our uploadFile
function will be an asynchronous function that uses the BlobServiceClient
to get a reference to the container, create a blob, and upload the data to that blob.
:bulb: Be sure to
return
a string like "File Saved" from this function when the file has been uploaded!
module.exports
main function:exclamation: Name your image file as test.png
or test.jpg
(depending on the submitted file extension) in our code for testing purposes.
You'll need to add your Blob URL to the github repository as a secret so we can test it! Name our secret to blob_url
and set it equal to your blob url, which should look like "https://bunnimagestorage.blob.core.windows.net". To find your url, simply place your storage account name in this template:
https://<your account name>.blob.core.windows.net
Week 3 Step 3 ⬤⬤⬤◯◯◯◯◯◯ | 🕐 Estimated completion: 10-20 minutes
This week, you will be going through steps to handle POST requests with no data.
codename
and uses it to name the image.
uploadFile()
functionbunnimage/index.js
to the bunnimage
branchTo test your work, use Postman to send a POST request without an image attached. You should see a response similar to the below:
{
"body" : "Sorry! No image attached."
}
💡 Yay! This means the error was successfully caught.
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.
:bulb: Hint: You'll need to code the
uploadFile
function another parameter since it now has to receive the body, extension, AND filename!Headers
Headers are yet another way to pass on information in HTTP requests. Here's how to access them:
var the_header_value = req.headers['insert_header_name'];
In this case, the header value will be used to name our picture.
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.
bunnimage-timer/index.js
on the bunnimage
branch and comment an expression that tells the Timer Trigger to run everyday at 6:23 AM.: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 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.
23 6 /1 /1 */1
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.
starter function