atst/js/lib/upload.js
dandds 0517bd1b0a Fix task order uploads.
Task order uploads and downloads were broken. Uploads were broken
because file content was eing read in as plaintext every time, resulting
in encoding issues. I updated the bundle to use the newer Azure JS SDK.
We can now use a method for uploading browser files directly without
having to read their content. This required a few small internal changes
to the upload component, since the response structure is different.

I also removed the `downloadUrl` method from the JS uploader since it
was not being used.

Downloads were broken because the method that generates the download
link was not updated to use the BlobSasPermissions class from the
updated Azure Python SDK.
2020-02-03 09:56:02 -05:00

57 lines
1.6 KiB
JavaScript

import { BlobServiceClient } from '@azure/storage-blob'
import 'whatwg-fetch'
class AzureUploader {
constructor(accountName, containerName, sasToken, objectName) {
this.accountName = accountName
this.containerName = containerName
this.sasToken = sasToken.token
this.objectName = objectName
}
async upload(file) {
const blobServiceClient = new BlobServiceClient(
`https://${this.accountName}.blob.core.windows.net?${this.sasToken}`
)
const containerClient = blobServiceClient.getContainerClient(
this.containerName
)
const blobClient = containerClient.getBlockBlobClient(this.objectName)
const options = {
blobHTTPHeaders: {
blobContentType: 'application/pdf',
},
metadata: {
filename: file.name,
},
}
return blobClient.uploadBrowserData(file, options)
}
}
export class MockUploader {
constructor(token, objectName) {
this.token = token
this.objectName = objectName
}
async upload(file, objectName) {
// mock BlobUploadCommonResponse structure: https://docs.microsoft.com/en-us/javascript/api/@azure/storage-blob/blobuploadcommonresponse?view=azure-node-latest
return Promise.resolve({ _response: { status: 201 } })
}
}
export const buildUploader = (token, objectName) => {
const cloudProvider = process.env.CLOUD_PROVIDER || 'mock'
if (cloudProvider === 'azure') {
return new AzureUploader(
process.env.AZURE_ACCOUNT_NAME,
process.env.AZURE_CONTAINER_NAME,
token,
objectName
)
} else {
return new MockUploader(token, objectName)
}
}