Conversation
|
Week 3 Step 3 ⬤⬤⬤◯◯◯◯◯◯ | 🕐 Estimated completion: 10-20 minutes Codename Blob + 3RR0R HandlingThis week, you will be going through steps to handle POST requests with no data. ✅ Task:
🚧 Test Your WorkTo 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."
}
1: Modify the FunctionHandle Empty DataNow we'll want to handle cases where the POST request's body happens to be empty. In your original ❓ How do I catch empty POST requests?Use an if-else statement to catch when var responseMessage = ""
if (body == null) {
responseMessage = "Sorry! No image attached."
} else {
var password = // get the header called "codename"
// use parse-multipart to parse the body
// determine the file-type here!
responseMessage = await uploadFile(parsedBody, ext, ?);
}
HeadersHeaders 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 Your time's up....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. ✅ Task:
🚧 Test Your WorkTo 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.
1. Create your Timer Trigger FunctionFirst, we'll need to create a new Azure Function that runs on a schedule with the Timer Trigger template.
1. Code your Timer Trigger Function
❓ How do I create the global variables above?To reference the const { BlobServiceClient } = require("@azure/storage-blob");To reference your connection string and account name: const connectionstring = process.env["AZURE_STORAGE_CONNECTION_STRING"];
const account = "YOUR_ACCOUNT_NAME";To delete all the blobs in the container, you can first list all the blobs, then loop through deleting each one as you go. ❓ How do I delete a blob?First, your Inside your function, create a const blobServiceClient = await BlobServiceClient.fromConnectionString(connectionstring);Create a variable that references the name of the container that contains the file you want to delete. const deletecontainer = "images";Fetch the container with that name. const deletecontainerClient = await blobServiceClient.getContainerClient(deletecontainer);Within that container, fetch the block blob client that has the name of const deleteblockBlobClient = deletecontainerClient.getBlockBlobClient(filename);Download the blob from the system and fetch a reference to the readable stream. const downloadBlockBlobResponse = await deleteblockBlobClient.download(0); // 0 refers to the position of the blob to downloadDelete the blob. const blobDeleteResponse = deleteblockBlobClient.delete();Set and return result = {
body : {
deletename: filename,
success: true
}
};
return result;❓ How do I call the `deleteBlob` function within `module.exports` and loop through existing blobs?Exactly like the beginning of your
const blobServiceClient = await BlobServiceClient.fromConnectionString(connectionstring);
const deletecontainer = "images";
const deletecontainerClient = await blobServiceClient.getContainerClient(deletecontainer);Now you'll want to use the for await (const blob of deletecontainerClient.listBlobsFlat()) {
context.log('\t', blob.name);
await deleteBlob(blob.name)
// access the blob's name and call deleteBlob to delete it!
}You can also add a log after your for loop that notifies you that all the blobs have been deleted. context.log("Just deleted your blobs!") |


No description provided.