Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/fsharp/using-fsharp-on-azure/blob-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ description: Store unstructured data in the cloud with Azure Blob Storage.
author: sylvanc
ms.date: 09/17/2024
ms.custom: "devx-track-fsharp"
ai-usage: ai-assisted
---
# Get started with Azure Blob Storage using F\#

Azure Blob Storage is a service that stores unstructured data in the cloud as objects/blobs. Blob storage can store any type of text or binary data, such as a document, media file, or application installer. Blob storage is also referred to as object storage.

This article shows you how to perform common tasks using Blob storage. The samples are written using F# using the Azure Storage Client Library for .NET. The tasks covered include how to upload, list, download, and delete blobs.

For a conceptual overview of blob storage, see [the .NET guide for blob storage](/azure/storage/blobs/storage-quickstart-blobs-dotnet). For ease, these tutorials use [connection strings](/azure/storage/storage-configure-connection-string) to authenticate with Azure. For optimal security, you should use Microsoft Entra ID with [managed identities](/entra/identity/managed-identities-azure-resources/).
For a conceptual overview of blob storage, see [the .NET guide for blob storage](/azure/storage/blobs/storage-quickstart-blobs-dotnet). For ease, these tutorials use [connection strings](/azure/storage/storage-configure-connection-string) to authenticate with Azure. For production applications, you should use Microsoft Entra ID with [managed identities](/entra/identity/managed-identities-azure-resources/) or the [Azure.Identity library](/dotnet/api/overview/azure/identity-readme) for enhanced security.

## Prerequisites

Expand Down Expand Up @@ -90,7 +91,7 @@ To upload a file to a block blob, get a container client and use it to get a blo

## List the blobs in a container

To list the blobs in a container, first get a container reference. You can then use the container's `GetBlobs` method to retrieve the blobs and/or directories within it. To access the rich set of properties and methods for a returned `BlobItem`.
To list the blobs in a container, first get a container reference. You can then use the container's `GetBlobsByHierarchy` method to retrieve the blobs and/or directories within it. This method returns `BlobItem` objects that provide access to blob properties and metadata.

[!code-fsharp[BlobStorage](../../../samples/snippets/fsharp/azure/blob-storage.fsx#L57-L58)]

Expand Down Expand Up @@ -134,7 +135,7 @@ To delete a blob, first get a blob reference and then call the

If you are listing a large number of blobs, or you want to control the number of results you return in one listing operation, you can list blobs in pages of results. This example shows how to return results in pages.

This example shows a hierarchical listing, by using the `GetBlobsByHierarchy` method of the `BlobClient` .
This example shows a hierarchical listing, by using the `GetBlobsByHierarchy` method of the `BlobContainerClient`.

[!code-fsharp[BlobStorage](../../../samples/snippets/fsharp/azure/blob-storage.fsx#L88-L100)]

Expand Down
9 changes: 4 additions & 5 deletions samples/snippets/fsharp/azure/blob-storage.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ let ListBlobsSegmentedInHierarchicalListing(container:BlobContainerClient) =

// Create some dummy data by uploading the same file over and over again
for i in 1 .. 100 do
let blob = container.GetBlobClient($"myblob{i}.txt")
use fileStream = System.IO.File.OpenRead(localFile)
let blob = container.GetBlobClient($"myblob{i}.txt")
blob.Upload(localFile)

ListBlobsSegmentedInHierarchicalListing container
Expand All @@ -122,8 +121,8 @@ appendContainer.CreateIfNotExists() |> ignore
let appendBlob = appendContainer.GetAppendBlobClient("append-blob.log")

// Create the append blob. Note that if the blob already exists, the
// CreateOrReplace() method will overwrite it. You can check whether the
// blob exists to avoid overwriting it by using CloudAppendBlob.Exists().
// CreateIfNotExists() method will overwrite it. You can check whether the
// blob exists to avoid overwriting it by using appendBlob.Exists().
appendBlob.CreateIfNotExists()

let numBlocks = 10
Expand All @@ -142,5 +141,5 @@ for i in 0 .. numBlocks - 1 do
appendBlob.AppendBlock(stream)

// Read the append blob to the console window.
let downloadedText = appendBlob.DownloadContent().ToString()
let downloadedText = appendBlob.DownloadContent().Value.Content.ToString()
printfn $"{downloadedText}"