Azure Blob Storage and Node: Creating Blobs
This is part of a series on working with Azure Blob Storage in Node.
- Introduction
- First Steps
- Creating Blobs
- Downloading Blobs
- Listing Blobs
- Blob Metadata
- All Together
Uploading to Azure Blob Storage
In the previous post, we talked about how to set up and connect to an Azure Storage account, and how to create a blob container. Today we’ll discuss how to upload blobs.
Account Connection Boilerplate
To upload to blob storage, you first need to create a client instance. To avoid duplicating code over and over, we’ll do that once here, and assume it in the other code samples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Now for the rest of the code samples, we’ll assume that they’re replacing the “Your code goes here” placeholder in the code above.
Uploading a String
Uploading a string is easy with the createBlockBlobFromText
method.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The above code creates a blob called “my-awesome-text-blob” inside our container with the content “Hello, World!”.
Uploading a File
Uploading a file is just as simple as uploading a string. Use the createBlockBlobFromFile
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
The above code creates a blob called “my-awesome-file-blob” inside our container with the contents of “hello-world.txt”.
Uploading a Stream
Again, uploading streams is easy with azure-storage.
Just use the createBlockBlobFromStream
method.
The one caveat here is that you need to know the length of the stream you’re uploading.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Uploading an HTTP Response
You can also download a file and upload it to blob storage. This is useful for things like caching API responses.
For this, we’ll use createWriteStreamToBlockBlob
to get a writable stream that saves to a blob.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
This example is a bit more complex, but the main idea is to create a write stream for the blob we want to create.
Then we fetch http://example.com/index.html and .pipe
the response to the blob’s write stream;
If you’re doing this in a web server, you’ll probably also want to pipe the http.get response to the client.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Setting a Content Type
You may have noticed that the previoius example passed an extra option to createWriteStreamToBlockBlob
.
There are a number of properties you can set on a blob, but the most useful is the contentType property.
This determines the Content-Type header that will be set if the blob is accessed or streamed directly from blob storage.
By default the content type is set to ‘application/octet-stream’. If you create a blob with this type and try to access it with your browser, it will simply be downloaded rather than displayed as the desired type (text, html, image).
To set a custom content type, pass something like { contentSettings: { contentType: 'image/jpeg' } }
before your callback function.
Note that createBlockBlobFromFile
will infer the content type from the file extension.
There are a number of other properties that can be set on a blob, including contentEncoding, contentLanguage, and metadata (which will be covered in a later post).
The easiest way to see the different options is to look in lib\services\blob\blobservice.js inside the azure-storage package.
Conclusion
In this post we’ve covered the many ways to create Azure blobs in Node. Next time we’ll talk about downloading blobs.