Basic file upload to Azure BlockBLob
This commit is contained in:
1 parent
9ab6a337d3
commit
ac5e253d02
9 files changed
+850
-84
No files matched your search
+73
-1
@@ -7,11 +7,32 @@ const {
|
||||
} = require('prisma-binding')
|
||||
const { makeExecutableSchema } = require('graphql-tools')
|
||||
const { importSchema } = require('graphql-import')
|
||||
const parser = require('fast-xml-parser')
|
||||
|
||||
const checkJwt = require('./auth/middleware/jwt')
|
||||
const { getMember } = require('./auth/middleware/getMember')
|
||||
const { validateIdToken } = require('./auth/validateIdToken')
|
||||
const { directiveResolvers } = require('./auth/authDirectives')
|
||||
const {
|
||||
createStoragePipline,
|
||||
createServiceURL,
|
||||
listContainers,
|
||||
createContainer,
|
||||
createBlob,
|
||||
uploadBlob,
|
||||
downloadBlob,
|
||||
deleteBlob,
|
||||
deleteContainer
|
||||
} = require('./storage/azure')
|
||||
//var azure = require('azure-storage')
|
||||
const Azure = require("@azure/storage-blob")
|
||||
const azureAccount = process.env.AZURE_STORAGE_ACCOUNT
|
||||
const azureKey = process.env.AZURE_STORAGE_ACCESS_KEY
|
||||
const azureStorageUrl = process.env.AZURE_STORAGE_URL
|
||||
|
||||
var debug = require('debug')('http')
|
||||
, http = require('http')
|
||||
, name = 'My App';
|
||||
|
||||
const TEAMS_FRAGMENT = require('./graphql/TeamsFragment')
|
||||
const TEAMS_CONNECTIONS_FRAGMENT = require('./graphql/TeamsConnectionsFragment')
|
||||
@@ -115,6 +136,57 @@ const resolvers = {
|
||||
return error.message
|
||||
})
|
||||
})
|
||||
},
|
||||
async getAzureSAS(parent, { teamSlug }, ctx, info) {
|
||||
// Create start and expiry times
|
||||
let today = new Date()
|
||||
let tomorrow = new Date()
|
||||
tomorrow.setDate(today.getDate() + 7)
|
||||
// Create SharedKeyCredential and attach to pipline
|
||||
const SKC = new Azure.SharedKeyCredential(azureAccount, azureKey)
|
||||
const pipeline = Azure.StorageURL.newPipeline(SKC)
|
||||
// Create container URL
|
||||
const serviceURL = new Azure.ServiceURL(azureStorageUrl, pipeline)
|
||||
const containerName = teamSlug
|
||||
const containerURL = Azure.ContainerURL.fromServiceURL(serviceURL, containerName)
|
||||
|
||||
let marker
|
||||
let containerCheck = null
|
||||
do {
|
||||
const listContainersResponse = await serviceURL.listContainersSegment(
|
||||
Azure.Aborter.None,
|
||||
marker,
|
||||
)
|
||||
console.log(`ContainerCheck marker: `, listContainersResponse)
|
||||
marker = listContainersResponse.marker;
|
||||
for (const container of listContainersResponse.containerItems) {
|
||||
console.log(`ContainerCheck: ${container.name} | ${containerName} | ${marker}`)
|
||||
if(container.name === containerName){
|
||||
containerCheck = container.name
|
||||
}
|
||||
}
|
||||
} while (marker)
|
||||
let newContainer
|
||||
if(containerCheck === null){
|
||||
newContainer = await containerURL.create(Azure.Aborter.None, { access: 'blob' })
|
||||
}
|
||||
console.log('getAzureSAS container: ', containerURL, containerCheck, newContainer)
|
||||
|
||||
// Set permissions for service, resource types and containers
|
||||
const SASServicePerms = Azure.AccountSASServices.parse('b')
|
||||
const SASResourceTypes = Azure.AccountSASResourceTypes.parse('co')
|
||||
const SASContainerPerms = Azure.ContainerSASPermissions.parse('rwl')
|
||||
// Generate SAS url
|
||||
const SASQueryParameters = Azure.generateAccountSASQueryParameters(
|
||||
{
|
||||
containerName,
|
||||
permissions: SASContainerPerms,
|
||||
startTime: today,
|
||||
expiryTime: tomorrow,
|
||||
services: SASServicePerms,
|
||||
resourceTypes: SASResourceTypes
|
||||
}, SKC )
|
||||
return SASQueryParameters
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -148,7 +220,7 @@ server.express.use(
|
||||
server.options.endpoint,
|
||||
checkJwt,
|
||||
function (err, req, res, next) {
|
||||
console.log('checkJwt: ', err, req)
|
||||
// console.log('checkJwt: ', err, req)
|
||||
if (err) {
|
||||
return res.status(201).send(err.message)
|
||||
} else {
|
||||
|
||||
@@ -21,6 +21,7 @@ type Mutation {
|
||||
createTeam(name: String!, slug: String!, owner: String!): Team @isAuthenticated
|
||||
updateTeamProfile(slug: String!, owner: String!, description: String, motto: String, avatar: String): Team @isAuthenticated
|
||||
deleteTeam(slug: String!): Team @isAuthenticated @isOwner(type: "Team")
|
||||
getAzureSAS(teamSlug: String!):String
|
||||
}
|
||||
|
||||
input MemberProfileInput {
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
const Azure = require('@azure/storage-blob')
|
||||
|
||||
/* Create Azure Storage pipeline
|
||||
* @accountName: Azure Blob Storage account name (required) - Environment variable
|
||||
* @accountKey: Azure Blob Storage storage key (required) - Environment variable
|
||||
*/
|
||||
const createStoragePipline = async (accountNAme, accountKey) => {
|
||||
return Azure.StorageURL.newPipeline(
|
||||
new Azure.SharedKeyCredential(accountName, accountKey)
|
||||
)
|
||||
}
|
||||
|
||||
/* Create Azure Storage Service URL
|
||||
* @storageURL: URL for Azure Storage endpoint (required) - Environment variable
|
||||
* @pipeline: Azure Storage Pipline (required) - created using createStoragePipeline()
|
||||
*/
|
||||
const createServiceURL = async (storageURL, pipeline) => {
|
||||
return new Azure.ServiceURL(
|
||||
storageURL,
|
||||
pipeline
|
||||
)
|
||||
}
|
||||
|
||||
/* List containers
|
||||
* @pipeline: Azure Storage Pipline (required) - created using createServiceURL
|
||||
* @service_url: URL for Azure Storage endpoint (required) - Environment variable
|
||||
*/
|
||||
const listContainers = async (serviceURL) => {
|
||||
let marker;
|
||||
do {
|
||||
const listContainersResponse = await serviceURL.listContainersSegment(
|
||||
Azure.Aborter.None,
|
||||
marker,
|
||||
);
|
||||
|
||||
marker = listContainersResponse.marker;
|
||||
for (const container of listContainersResponse.containerItems) {
|
||||
console.log(`Container: ${container.name}`);
|
||||
}
|
||||
} while (marker);
|
||||
}
|
||||
|
||||
|
||||
/* Create containers
|
||||
* @serviceURL: URL for Azure Storage endpoint (required) - created using createServiceURL
|
||||
* @containerName: Container Name
|
||||
* Returns: containerURL
|
||||
*/
|
||||
const createContainer = async (serviceURL, containerName) => {
|
||||
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName)
|
||||
|
||||
const createContainerResponse = await containerURL.create(Aborter.None)
|
||||
|
||||
console.log(
|
||||
`Created container ${containerName} successfully`,
|
||||
await createContainerResponse.requestId
|
||||
)
|
||||
return containerURL
|
||||
}
|
||||
|
||||
/* Create containers
|
||||
* @containerURL: continer URL (required) - created using createContainer
|
||||
* @blobName: Blob Name
|
||||
* Returns: blobURL
|
||||
*/
|
||||
const createBlob = async (containerURL, blobName) => {
|
||||
const blobURL = Azure.BlobURL.fromContainerURL(containerURL, blobName)
|
||||
console.log(`created blob successfully`, await blobURL)
|
||||
return blobURL
|
||||
}
|
||||
|
||||
/* Upload Blob
|
||||
* @blobURL: blobURL (required) - created using createBlob
|
||||
* @blobContent: Blob Content
|
||||
* Returns: uploadBlobResponse
|
||||
*/
|
||||
const uploadBlob = async (blobURL, blobContent) => {
|
||||
const blockBlobURL = Azure.BlockBlobURL.fromBlobURL(blobURL)
|
||||
const uploadBlobResponse = await blockBlobURL.upload(
|
||||
Azure.Aborter.None,
|
||||
blobContent,
|
||||
blobContent.length
|
||||
);
|
||||
console.log(`Upload block blob ${blobName} successfully`, await uploadBlobResponse)
|
||||
return uploadBlobResponse
|
||||
}
|
||||
|
||||
/* Download Blob
|
||||
* @blobURL: blobURL (required) - created using createBlob
|
||||
* @blobContent: Blob Content
|
||||
* Returns: downloadBlockBlobResponse
|
||||
*/
|
||||
const downloadBlob = async (blobURL, blobContent) => {
|
||||
const downloadBlockBlobResponse = await blobURL.download(Azure.Aborter.None, 0)
|
||||
console.log(
|
||||
"Downloaded blob content",
|
||||
downloadBlockBlobResponse.readableStreamBody.read(content.length).toString()
|
||||
)
|
||||
console.log(`BlockBlobResponse: ${downloadBlockBlobResponse}`)
|
||||
return downloadBlockBlobResponse
|
||||
}
|
||||
|
||||
/* Delete Blob
|
||||
* @blobURL: blobURL (required) - created using createBlob
|
||||
* Returns: bool
|
||||
*/
|
||||
const deleteBlob = async (blobrURL) => {
|
||||
await blobrURL.delete(Azure.Aborter.None);
|
||||
console.log("deleted blob", blobrURL);
|
||||
return true
|
||||
}
|
||||
|
||||
/* Delete Container
|
||||
* @containerURL: continer URL (required) - created using createContainer
|
||||
* Returns: bool
|
||||
*/
|
||||
const deleteContainer = async (containerURL) => {
|
||||
await containerURL.delete(Azure.Aborter.None);
|
||||
console.log("deleted container", containerURL);
|
||||
return true
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createStoragePipline,
|
||||
createServiceURL,
|
||||
listContainers,
|
||||
createContainer,
|
||||
createBlob,
|
||||
uploadBlob,
|
||||
downloadBlob,
|
||||
deleteBlob,
|
||||
deleteContainer
|
||||
}
|
||||
Reference in new issue
Block a user