page_type | languages | name | description | products | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
|
JavaScript end-to-end client file upload to Azure Storage Blobs |
Locally build and deploy client application to an Azure Static Web App with a GitHub action, analyze image with Cognitive Services Computer Vision. |
|
This sample project is a TypeScript React (create-react-app) framework client app with an HTML form to select a file for upload to Azure Storage Blobs.
TypeScript version
The user:
-
selects an image from the file system
-
uploads the image to Storage Blobs
-
Read Tutorial - The tutorial demonstrates how to load and run the project locally with VSCode. The tutorial includes creating a Storage resource, SAS token and CORS configuration.
The React (create-react-app) client app consists of the following elements:
- React app hosted on port 3000
- uploadToBlob.ts using @azure/storage-blob client library to create Blob container and upload file
This project framework provides the following features:
- Create Azure Storage resource
- Generate SAS token for Storage resource
- Set Storage resource CORS
- Select and upload file to Azure Storage Blob Container
-
Clone or download repo.
-
Create Azure Storage resource - using /scripts/newStorageService.js. This resource name is the
storageAccountName
. -
Generate SAS Token for Storage resource - using /scripts/az-storage-generte-sas.sh. This value is the
sasToken
. -
Configure CORS for browser - using /scripts/az-storage-cors-add.sh
Settings for CORS:
- Allowed origins:
*
- Allowed methods:
DELETE, GET, HEAD, MERGE, POST, OPTIONS, and PUT
- Allowed headers:
*
- Exposed headers:
*
- Max age:
86400
- Allowed origins:
-
Install dependencies:
npm install
To run the React app, you need the following Azure SDK client npm packages:
- @azure/identity
- @azure/storage-blob
A third Azure package, @azure/arm-storage, is listed in the
package.json
strictly for use by thescripts/newStorageService.js
file to create a new Azure Storage resource. -
Create a file name
.env
at the root of the project. -
Add two required variables with their storage values:
REACT_APP_AZURE_STORAGE_SAS_TOKEN= REACT_APP_AZURE_STORAGE_RESOURCE_NAME=
React builds the static files with these variables.
-
If the token begins with a question mark, remove the
?
. The code file provides the?
for you so you don't need it in the token. -
Start project:
npm start
-
View project in browser,
http://localhost:3000
. -
Select image then select
Upload!
.Page displays images in container.
- Git, if cloning
- Node.js and NPM
- Web browser
- Azure subscription to create resource on
-
Install the sample's dependencies:
npm install
-
Run the command to run the web app.
npm start
-
Open a web browser and use the following url to view the client app on your local computer.
http://localhost:3000/
If you received an error or your file doesn't upload to the container, check the following:
- Recreate your SAS token, making sure that your token is created at the Storage resource level and not the container level. Copy the new token into the code at the correct location.
- Check that the token string you copied into the code doesn't contain the
?
(question mark) at the beginning of the string. - Verify your CORS setting for your Storage resource.
- Create Azure Storage Blob from JavaScript file: scripts/newStorageService.js
- Set CORS for service using Azure CLI script: scripts/az-storage-cors-add.sh
- Generate SAS Token using Azure CLI script: scripts/az-storage-generate-sas.sh
The /images folder includes images for upload.