2. Uploading BLOBs
To upload files from your web page, you’re going to use the built-in ASP.NET upload control at in listing 1. On click of the upload button at ,
you’re going to capture the uploaded file and then upload the captured
file to BLOB storage. The following listing contains the code-behind for
the upload button click event.
Listing 3. Posting the uploaded file to BLOB storage
All the code in listing 3 has been discussed in previous examples, except for what’s happening at and . At
you get a reference to the BLOB that you’re about to create. We’re
giving the BLOB the original name of the uploaded file (retrieved from
the UploadFile control). Then, at you upload the new file to BLOB storage.
Tip
You’ll notice that
we’re extracting the filename and the file contents directly from the
ASP.NET file upload control. If you want, you can give the file a name
different from the original.
By default, ASP.NET is
configured to allow a maximum upload of 4 MB. If you provide a web role
frontend to the BLOB storage as we’ve done in this sample, you might
need to increase the maximum request length.
To increase the default value to a larger value, you need to add the following line under the system.web element in the web.config file:
<httpRuntime executionTimeout="300" maxRequestLength="51200"/>
The maximum upload size in the above example is 50 MB.
|
In the previous example, we used the UploadByteArray method to upload the BLOB. Three other methods are provided in the StorageClient library that you can use: UploadFile, UploadText, and UploadFromStream. Depending on your situation, one of these methods might be easier to use than UploadByteArray (for example, UploadFile might be a better choice if you have a local file on disk that you want to store in BLOB storage).
The maximum size of a BLOB is 1
TB, but if a file is larger than 64 MB, under the covers the
StorageClient library splits the file into smaller blocks of 4 MB each.
One of the advantages of the StorageClient library is that you don’t
need to worry about this. If you’re using the REST API, you’ll need to
implement the splitting of BLOBs into blocks and the committal of blocks
and retry logic associated with re-uploading failed blocks (yet another
good reason to use the StorageClient library).
If you’re a sick and
twisted individual who wants to mess around with blocks, then feel free
to look in more detail at the online documentation at http://msdn.microsoft.com/en-us/library/ee691964.aspx.
|
Now that you’ve spent all that time and effort adding the file to BLOB storage, let’s delete it (groan).
3. Deleting BLOBs
Deleting a BLOB is similar
to uploading a file except that you’re deleting the BLOB instead of
uploading it (cute, huh?). Just like the upload file example in listing 4, you get the reference to the BLOB, and then delete the BLOB by calling the following: blob.Delete();
The following listing shows the code that will delete the BLOB in your web page.
Listing 4. Deleting the BLOB
As
you can see, deleting a BLOB is pretty simple. Now you can list,
upload, and delete BLOBs from your storage account using your ASP.NET
management website hosted in your Windows Azure web role. Let’s complete
the management page example by looking at how you can download BLOBs.