emsesc / serverless-demo

GNU General Public License v3.0
0 stars 0 forks source link

Week 3: Your times up.... #29

Closed counselorbot[bot] closed 2 years ago

counselorbot[bot] commented 2 years ago

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 assists us in automated cleanups for our storage container and removing outdated files that users no longer need.

✅ Task:

💡 Tip: Cron expressions might help you with the last task.

🚧 Test Your Work

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 func start 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.


1: Create your Timer Trigger Function

First, we'll need to create a new Azure Function that runs on a schedule with the Timer Trigger template.

  1. Create a new Function, naming it bunnimage-timer and select Timer trigger

image

  1. Leave the Schedule as 0 */5 * * * * to have the function be triggered every 5 minutes.

image

  1. Press enter on your keyboard.

2: Code your Timer Trigger Function

❓ How do I create the global variables above? To reference the `@azure/storage-blob` package: ```js const { BlobServiceClient } = require("@azure/storage-blob"); ``` To reference your connection string: ```js const connectionstring = process.env["AZURE_STORAGE_CONNECTION_STRING"]; const account = "YOUR_STORAGE_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 `deleteBlob` function will have to be asynchronous, so its signature should look like `async function deleteBlob(filename)`. Inside your function, create a `BlobServiceClient` object that will be used to create a container client. ```js const blobServiceClient = await BlobServiceClient.fromConnectionString(connectionstring); ``` Create a variable that references the name of the container that contains the file you want to delete. ```js const deletecontainer = "images"; ``` Fetch the container with that name. ```js const deletecontainerClient = await blobServiceClient.getContainerClient(deletecontainer); ``` Delete the blob. ```js deletecontainerClient.deleteBlob(filename) ``` Set and return `result` with a progress statement on the blob's deletion. ```js 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 `deleteBlob` function, you'll want to: 1. Create a `BlobServiceClient` object using your connection string. 2. Create a variable that references the name of the container that contains the file you want to delete. 3. Fetch the container with that name. ```js const blobServiceClient = await BlobServiceClient.fromConnectionString(connectionstring); const deletecontainer = "images"; const deletecontainerClient = await blobServiceClient.getContainerClient(deletecontainer); ``` Now you'll want to use the [`listBlobsFlat`](https://docs.microsoft.com/en-us/javascript/api/@azure/storage-blob/listblobsflatsegmentresponse?view=azure-node-latest) function to retrieve a reference to an enumeration of your blobs. Loop through these blobs, and delete each one. ```js 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. ```js context.log("Just deleted your blobs!") ```
emsesc commented 2 years ago

dsfgfa

counselorbot[bot] commented 2 years ago

⚡️ You're ready for Serverless!

Move on to the next issue.

moveon

emsesc commented 2 years ago

test

emsesc commented 2 years ago

test

emsesc commented 2 years ago

closed via oauth