Creating an Images Gallery with Azure Blob Storage and React.js | Step-by-Step Guide
Published Aug 08 2023 12:00 AM 8,114 Views
Iron Contributor

kevin_comba_32-1691253098408.png

 

Our goal in this blog post is to demonstrate how to use Azure Blob Storage with React.js to create an image gallery. We will go over the capabilities, advantages, and best features of Azure Blob Storage. We will also discuss the different programming languages that can be used to build Azure Storage Blob clients, focusing on the JavaScript Azure Storage Blob client library.

What is Azure Blob Storage?

 

kevin_comba_33-1691253203753.png

 

Azure Blob Storage is a Microsoft Azure cloud-based object storage solution. It enables developers to use blobs to store and manage unstructured data such as documents, photos, videos, backups, and logs. Blobs can be grouped together to form containers, providing a scalable and cost-effective method for storing and retrieving massive amounts of data.

 

What can we do with Blob Storage?

Azure Blob Storage offers various capabilities, such as:

  • Azure Blobs: Storing and serving static content to web applications example Images and documents, Streaming video and audio and Storing and processing big data, logs, and telemetry.
  • Azure Files: Managed file shares for cloud or on-premises deployments.
  • Azure Queues: A messaging store for reliable messaging between application components.
  • Azure managed Disks: Block-level storage volumes for Azure VMs.
  • Storing backups and archives for disaster recovery.

 

Advantages of using Azure Blob Storage:

  • Scalability: Blob Storage can handle massive amounts of data, and you can easily scale storage as your needs grow.
  • Durability: Data stored in Blob Storage is redundantly backed up to ensure high availability and durability.
  • Accessibility: Data can be accessed securely from anywhere in the world using RESTful APIs or client libraries.
  • Cost-Effective: Blob Storage offers flexible pricing tiers, allowing you to optimize costs based on data access patterns.
  • Integration: Seamlessly integrate Blob Storage with other Azure services for a comprehensive cloud solution.

 

Some of the Best Features available in Azure Blob Storage

  • Storage Tiers: Choose between hot, cool, and archive tiers to match data access patterns and reduce costs.
  • Blob Lifecycle Management: Automatically manages data retention and deletion based on predefined policies.
  • Shared Access Signatures: Generate time-limited access tokens to grant temporary access to specific blobs.
  • Azure Functions Triggers: Trigger functions when new blobs are added or modified, enabling real-time processing.
  • Blob Versioning: Keep track of changes and revisions to blobs using versioning capabilities.

 

Programming Languages for Consuming Azure Blob Storage

  • Users or client applications can access objects in Blob Storage via HTTP/HTTPS, from anywhere in the world. Objects in Blob Storage are accessible via the Azure Storage REST API, Azure PowerShell, Azure CLI, or an Azure Storage client library. Client libraries are available for different languages, including:
  • JavaScript or (node.js): Utilize the Azure Storage Blob client library for JavaScript to interact with Blob Storage from web applications built with React.js or server frameworks.
  • C#(.Net): Use the Azure SDK for .NET to build .NET-based applications that interact with Blob Storage.
  • Python: The Azure SDK for Python enables Python developers to integrate Blob Storage functionality into their applications.
  • Java, Go, and more: Azure Blob Storage provides SDKs for multiple programming languages, enabling broad language support.

 

Procedure

  1. Provision Azure blob storage
  2. Clone our Client Application.
  3. Integrate Azure SDK in our Client Application.

 

Pre-requisites

 

Provision Azure blob storage

 

  1. Go to Azure portal and search for storage accountskevin_comba_64-1691253432999.png

     

  1. Create a new storage account. kevin_comba_82-1691253762165.png

     

  2. Select your subscription and create a resource groupkevin_comba_66-1691253433022.png

     

  1. Create a unique storage account name, select your region, performance go with standard and redundancy select Geo-redundant storage (GRS)kevin_comba_67-1691253433033.png

     

  1. Under security make sure the below options are selected. They will enable us to access the storage using REST API endpoints and Authentication.kevin_comba_68-1691253433039.png

     

 

  1. Under blob storage access tier select HOT, because we want to access our blob storage files frequently.kevin_comba_69-1691253433048.png

     

  1. Under network connectivity, for now lets go with enable public access but later I will show you how to change this. Also select Microsoft routing.kevin_comba_70-1691253433057.png

     

  1. Under recovery, lets enable soft delete, this is important when we want to recover our blob, container, or files.kevin_comba_71-1691253433069.png

     

  1. For encription, I will go with Microsoft-managed keys.kevin_comba_72-1691253433076.png

     

  • Create tags, give a name and value of your choice, the main aim of tags is to categorize resources and consolidate bills to one tag.kevin_comba_73-1691253433084.png

     

  • Review your options and create.kevin_comba_74-1691253433094.png

     

  • After deployment is completed, go to resource.kevin_comba_75-1691253433104.png

     

  • Under overview, you will find all the options related to our resource which we had earlier selected.kevin_comba_76-1691253433121.png

     

  • Click Containers and create a new container (think of a container as a folder where you can store related files like images). Give it a unique name and access level. I will go with private because I want to access my container using an SAS access token.kevin_comba_77-1691253433133.png

     

  • After successfully creating a container, you will see it displayed below.kevin_comba_78-1691253433144.png

     

  1. Under shared access signature, select allowed services, for this example we only need blob but select all if you like. Allowed resource type select container, service and object. Allowed permissions go with all options. Select blob versioning and blob indexing. Start and expiry date are important, because this is the lifetime of our SAS token to be valid. Select your time zone. I have opted not to add an IP address since I will access the local storage with a local server, but if your application is live get its IP address and it here so that only that IP address can access the storage account. Allowed protocols I will go with both HTPPS and HTTP since I’m running a local host client application. Lastly click Generate SAS and connection string.kevin comba (1).png

     

  • Copy the SAS token, we will use it in our react application in the environmental variables.kevin_comba_80-1691253433165.png

     

  • Under CORS, we need to enable the origin which is going to access our storage account, allowed methods example POST, GET, DELETE & PUT. Allowed headers and exposed header I have gone with * meaning all (it’s not a good practice). The max age is the amount of time the browser should cache the preflight Options in the request, I have chosen 100 seconds. More information about CORS.kevin_comba_81-1691253433177.png

     

 

 

Clone our Client Application.

 

  • Create a folder and open it using VSCode. Open the terminal on your VS code and type
    Git clone https://github.com/kelcho-spense/Image_Gallery_Azure_Blob_storage-Educator_Developer_Blog-.git . ​
    kevin_comba_83-1691253929066.png

     

  • Wait till the process is done.kevin_comba_84-1691253929076.png

     

  • Click on .env file and add your SAS token (the one we generated on step 17 above). If you used the values I used for Storage account and storage container, you do not need to change the values I have provided.kevin_comba_85-1691253929082.png

     

  • To install our node dependencies run yarn or npm install (if not using yarn as your package manager). Then run yarn run dev or npm run dev to start our local application server.kevin_comba_86-1691253929091.png

     

  • Click on the upload icon to upload an image, select image and click okey.Screenshot 2023-08-05 181200.png

     

  • After uploading successfully, an image should be displayed below. kevin_comba_90-1691253929109.png

     

  • After uploading several images you can even click the Delete button (red in color) to delete the image.kevin_comba_91-1691253929117.png

     

    kevin_comba_7-1691256377981.png

 

  • I have stored all the code in App.js file for simplicity reasons. The file starts with icons imports, some states (variables) and storage account credentials.kevin_comba_6-1691256027294.png

     

     

  • We have 3 main functions, fetchImages to fetch all images, handleSubmit to save images to azure blob storage, handleDelete to delete an images.Screenshot 2023-08-05 180444.png

     

  • This code below is just for uploading and displaying images on our web page.Screenshot 2023-08-05 180948.png

     

Read more about

 

Co-Authors
Version history
Last update:
‎Mar 08 2024 04:42 AM
Updated by: