In this video, explore the metadata key-value collection available on Azure Storage objects and, in particular files, in Azure storage files.
- [Instructor] One of the benefits of having this dual view of the file system, both in SMB access and in REST API access, is the addition of metadata. As you may have seen or even used with glob storage, you can add a collection of key value pairs, to objects, globs, or their containers, and this also applies to files. So in Azure storage files, we have the opportunity to read the file attributes, properties about the files, but also user-defined metadata.
And so we get to specify key value pairs to be applied to our files that we could read, independent of having to read the full content of an individual file. This is particularly useful for search and similar systems where we don't want to re-process a file after we have already extracted some metadata. For photos that might be EXIF data. For larger files, perhaps an ISO, might be a description of the software contained within and so forth. Let's take a look at Visual Studio's view of the metadata.
Returning to Visual Studio, we'll open our exercise files and create a new class. We'll name it the same as before, Tests, File, and in this case, six zero for metadata. And I will copy and paste mostly from the operations file. So from operations, double-click. We'll copy and paste the entire class in it method, as well as the two private static fields. Control C to copy, back to metadata, paste inside the class.
And I'll use the control dot to add the using statement for Class Initialize. And control dot by clicking on check for influent. Here on line 19, control dot, so using influent and then we have a few other storage credentials in line 25, control dot using Microsoft Windows as your storage oath, and similarly for cloud storage account, control dot, using Microsoft Windows Azure dot storage. One more on line 14, cloud file client, control dot, using Microsoft Windows Azure storage file.
So now we need to add our test method. So the first method is again async, returning task. (typing) And we'll call it Test 60 Metadata for task, control dot to add the using for the name space tasks. And now we will copy the code from operations again for an upload, so returning to the operations file.
For the method upload file, copy the body, line 39 through 32 in this case, control C, return to the other file and paste inside our new method, in my case, line 37. Control V, and so now we have a new test, control S to save, where in the method called Test 60 Metadata, we use the folder, the file share called photos, upload a photo, assuming it exists, control dot to system dot IO.
And at this point, we would have successfully uploaded the file. Now assuming that works for you, you have the file, you have previously set your test settings file, if all of that's in place, then this test should pass. Let's just run it to make sure. Control Shift B to bold, and my test is not showing, cause my class is not public yet, and it is a test class, Control Shift B to bold, and here we have here we have unitest. Now before we make any further modifications, run it to make sure it works, and then go to the Azure portal to confirm that your file has been uploaded.
I'll click Refresh, in my learn Azure today storage account. I'll go to photos, and I can see the Madagascar photo up here. Now that's the file upload we've had before. Now, we'll modify our method and add new code. So given that the file exists, we can add metadata to a key value collection called metadata on the cloud file. For example, for a photo I've taken, I may know the location of the photo, and if I was the photographer, I want to add my name there for copyright purposes.
And then we'll set the metadata on the object, on the file in this case, and we'll do so with an async method, so we will await that method. Now remember when we upload the file, we are replacing the existing file, and so we'll do the upload again, but this time, we'll add this metadata to the file, which we can then see in the portal. Control Shift S to save all, Control Shift B for bold, not on the test again, run selected test, Test 60 Metadata, finish this successfully.
Let's go back to the portal, my file is still there. I'll click Refresh, but this time, notice the ellipse at the end, select, and then click edit metadata. And so here we can see the location and the photographer key value pairs have been added to the file. This is incredibly useful when we do search and other types of operations, when we do not need the content of the file, we just need information about the file. So this is much more effective than downloading megabytes, gigabytes, terabytes with data, when we have previously extracted or previously obtained this metadata about the file.
And so in this video you saw how you can add metadata for additional properties to your files in Azure storage, so that would be very useful for search and other use cases. So do take a look at the Azure search course, also available in our library.
- Creating and managing storage accounts in the Azure portal
- Stored access policies for granting privileges
- Encrypting data at rest
- Azure Storage Files and net use
- Querying files
- Accessing files from both SMB and the REST API concurrently
- Using AzCopy to copy data among storage accounts asynchronously