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:
[x] 1: Create a Timer Trigger Function to delete all files in your Blob storage container every 5 minutes.
[x] 2: Test your new Timer Trigger Function
[x] 3:Don't commit your new code.
[ ] 4: Comment an expression that tells the Timer Trigger to run everyday at 6:23 AM.
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.
Create a new Function, naming it bunnimage-timer and select Timer trigger
Leave the Schedule as 0 */5 * * * * to have the function be triggered every 5 minutes.
Press enter on your keyboard.
2: Code your Timer Trigger Function
Reference your container string from your environment secrets
Reference the @azure/storage-blob package
❓ 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!")
```
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:
🚧 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:
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.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.
bunnimage-timer
and selectTimer trigger
Schedule
as0 */5 * * * *
to have the function be triggered every 5 minutes.2: Code your Timer Trigger Function
@azure/storage-blob
package❓ 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!") ```