Created
July 23, 2020 22:45
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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