Skip to content

Instantly share code, notes, and snippets.

@haydenbr
Created July 23, 2020 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haydenbr/332545980e978119be9b2d1b1152bb56 to your computer and use it in GitHub Desktop.
Save haydenbr/332545980e978119be9b2d1b1152bb56 to your computer and use it in GitHub Desktop.
Use a generator function to partition a blob and upload blocks sequentially to Azure Storage
async function putBlocks(blob: Blob, url: string): Promise<string[]> {
const blockIds: string[] = []
let baseUrl = url + '&comp=block'
for (let { block, blockId } of partitionBlob(blob, BLOCK_SIZE)) {
blockIds.push(blockId)
await fetch(`${baseUrl}&blockid=${blockId}`, {
...DEFAULT_FETCH_SETTINGS,
headers: {
'x-ms-blob-type': 'BlockBlob'
},
body: block
})
}
return blockIds
}
function* partitionBlob(blob: Blob, partitionSize: number): Generator<{ block: Blob, blockId: string }, void> {
const totalBlocks = Math.ceil(blob.size / partitionSize)
let blockPointer = 0
for (let blockIndex = 0; blockIndex < totalBlocks; blockIndex++) {
let nextBlockPointer = blockPointer + partitionSize
yield {
block: blob.slice(blockPointer, Math.min(nextBlockPointer, blob.size)),
blockId: getBlockId(blockIndex)
}
blockPointer = nextBlockPointer
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment