budibase/packages/datastores/datastores/azure-blob.js

82 lines
2.1 KiB
JavaScript
Raw Normal View History

import {
SharedKeyCredential,
BlockBlobURL,
BlobURL,
ContainerURL,
ServiceURL,
StorageURL,
Aborter,
} from "@azure/storage-blob"
export const createFile = ({ containerUrl }) => async (key, content) => {
2020-02-25 16:21:23 +01:00
const blobURL = BlobURL.fromContainerURL(containerUrl, key)
const blockBlobURL = BlockBlobURL.fromBlobURL(blobURL)
await blockBlobURL.upload(Aborter.none, content, content.length)
2019-06-07 15:18:10 +02:00
}
export const updateFile = opts => async (path, content) =>
createFile(opts)(path, content)
2019-06-07 15:18:10 +02:00
2020-02-25 16:21:23 +01:00
export const loadFile = ({ containerUrl }) => async (key, content) => {
const blobURL = BlobURL.fromContainerURL(containerUrl, key)
2019-06-07 15:18:10 +02:00
const downloadBlockBlobResponse = await blobURL.download(Aborter.none, 0)
2019-06-07 15:18:10 +02:00
return downloadBlockBlobResponse.readableStreamBody
.read(content.length)
.toString()
}
2019-06-07 15:18:10 +02:00
2020-02-25 16:21:23 +01:00
export const exists = ({ containerUrl }) => async key => {
const blobURL = BlobURL.fromContainerURL(containerUrl, key)
const getPropsResponse = await blobURL.getProperties()
return getPropsResponse._response.StatusCode === 200
}
2019-06-07 15:18:10 +02:00
export const deleteFile = ({ containerURL }) => async key => {
const blobURL = BlobURL.fromContainerURL(containerURL, key)
await blobURL.delete(Aborter.none)
}
2019-06-07 15:18:10 +02:00
export const createContainer = ({ containerUrl }) => async () =>
await containerUrl.create(Aborter.none)
2019-06-07 15:18:10 +02:00
export const deleteContainer = ({ containerUrl }) => async () =>
await containerUrl.delete(Aborter.none)
2019-06-07 15:18:10 +02:00
const initialise = opts => {
const sharedKeyCredential = new SharedKeyCredential(
opts.account,
opts.accountKey
)
const pipeline = StorageURL.newPipeline(sharedKeyCredential)
const serviceURL = new ServiceURL(
2020-02-25 16:21:23 +01:00
`https://${opts.account}.blob.core.windows.net`,
pipeline
)
const containerURL = ContainerURL.fromServiceURL(
serviceURL,
opts.containerName
)
return {
containerURL,
}
}
2019-06-07 15:18:10 +02:00
export default opts => {
const access = initialise(opts)
return {
createFile: createFile(access),
updateFile: updateFile(access),
loadFile: loadFile(access),
exists: exists(access),
datastoreType: "azure-blob-storage",
datastoreDescription: "",
}
}