-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlobStorage.jsx
123 lines (112 loc) · 4.46 KB
/
BlobStorage.jsx
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { useEffect, useState } from 'react';
import { getBlobServiceClient } from '../azureManagement';
import { protectedResources, storageInformation } from '../authConfig';
import { useMsal, useMsalAuthentication } from '@azure/msal-react';
import { InteractionType } from '@azure/msal-browser';
import Button from 'react-bootstrap/Button';
import Form from 'react-bootstrap/Form';
import { Container, Row, Alert } from 'react-bootstrap';
export const BlobStorage = () => {
const { instance } = useMsal();
const [uploadedFile, setUploadedFile] = useState(null);
const [message, setMessage] = useState('');
const [showMessage, setShowMessage] = useState(false);
const account = instance.getActiveAccount();
const request = {
scopes: protectedResources.armBlobStorage.scopes,
account: account,
};
const { login, result, error } = useMsalAuthentication(InteractionType.Popup, {
...request,
redirectUri: '/redirect',
});
useEffect(() => {
if (!!error) {
// in case popup is blocked, use redirect instead
if (error.errorCode === 'popup_window_error' || error.errorCode === 'empty_window_error') {
login(InteractionType.Redirect, request);
}
console.log(error);
return;
}
// eslint-disable-next-line
}, [login, result, error, instance]);
const containerExist = async (client, containerName) => {
let hasContainer = false;
for await (const container of client.listContainers()) {
if (container.name === containerName) {
hasContainer = true;
}
}
return hasContainer;
};
const handleAlert = () => {
setMessage('');
setShowMessage(false);
};
const handleSubmit = async (e) => {
e.preventDefault();
if (uploadedFile) {
try {
const client = await getBlobServiceClient(result.accessToken);
const containerClient = client.getContainerClient(storageInformation.containerName);
const hasContainer = await containerExist(client, storageInformation.containerName);
if (hasContainer) {
const blockBlobClient = containerClient.getBlockBlobClient(uploadedFile.name);
await blockBlobClient.uploadData(uploadedFile);
setMessage('File uploaded successfully');
} else {
const createContainerResponse = await containerClient.create();
const blockBlobClient = containerClient.getBlockBlobClient(uploadedFile.name);
await blockBlobClient.uploadData(uploadedFile);
console.log('Container was created successfully', createContainerResponse.requestId);
setMessage('Update file and created container successfully');
}
setShowMessage(true);
} catch (error) {
console.log(error);
setMessage("couldn't upload file");
setShowMessage(true);
}
}
};
const handleFileUpload = (e) => {
e.preventDefault();
setMessage('');
setShowMessage(false);
setUploadedFile(e.target.files[0]);
};
if (error) {
return <div>Error: {error.message}</div>;
}
return (
<Container>
<Row>
<div className="data-area-div">
<p>
Acquired an <strong>Access Token </strong>for Azure Storage with scope:
<mark>{protectedResources.armBlobStorage.scopes}</mark>
</p>
<p>Upload a file to Azure Storage</p>
</div>
</Row>
<Row>
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3">
<Form.Control type="file" placeholder="add file" onChange={handleFileUpload} />
</Form.Group>
<Button type="submit">Upload</Button>
</Form>
</Row>
{showMessage ? (
<Row>
<div className="data-area-div" onClick={handleAlert}>
<Alert variant="success">
<p>{message}</p>
</Alert>
</div>
</Row>
) : null}
</Container>
);
};