Created
November 20, 2019 21:00
-
-
Save briglx/66b2c478ab531bf3d9fd123cafaa967e to your computer and use it in GitHub Desktop.
Buggy Example of Copying Blob files
This file contains 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
connection_string = "connection string to my dest blob storage account" | |
container_name = "myContainerName" | |
dest_file_name = "myDestFile.csv" | |
remote_blob_url = "http://path/to/remote/blobfile.csv" | |
client = BlobServiceClient.from_connection_string(connection_string) | |
dest_blob = client.get_blob_client(container_name, dest_file_name) | |
resp = requests.get(blob_url, stream=True) | |
total_size = int(resp.headers.get("content-length", 0)) | |
chunk_size = 10 * 10 * 10 * 10 * 1024 # 10.24 MB | |
#Upload empty file | |
copied_blob.upload_blob(b'') | |
i = 0 | |
running = 0 | |
prog = progress(total=total_size, unit="iB", unit_scale=True) | |
for step in range(total_size, 0, -chunk_size): | |
offset = total_size - step | |
length = chunk_size | |
if step < chunk_size: | |
length = step | |
# this will only stage your block | |
dest_blob.stage_block_from_url( | |
block_id = i+1, | |
source_url = blob_url, | |
source_offset= offset, | |
source_length = length ) | |
# now it is committed | |
dest_blob.commit_block_list([i+1]) | |
running += length | |
i += 1 | |
committed, uncommitted = dest_blob.get_block_list('all') | |
assert(total_size, running) # Passes | |
assert(total_size, len(committed)) # Len of committed is only 1, running is 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment