Created
June 27, 2014 19:03
-
-
Save tcotav/f98ed7f7c4bc2fc6877d to your computer and use it in GitHub Desktop.
Working around the chef azure cookbook -- no blob download method... here, let me write one for you :-/ Will need to rewrite to integrate with actual cookbook.
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
require 'azure' | |
# run looks like this | |
# | |
# $ ruby get_blob.rb | |
# write-pdf - length: 9613706, last-modified: Fri, 27 Jun 2014 18:28:49 GMT | |
# Target content length: 9613706 | |
# --- progress 43.63 percent, time: 29.08 s | |
# --- progress 87.26 percent, time: 26.68 s | |
# --- progress 100.00 percent, time: 25.49 s | |
# | |
# Total dl time: 81.25 seconds | |
# Avg dl rate: 0.1128 MBytes/sec | |
MB=1024*1024 | |
CHUNK_MAX=4*MB | |
storage_account = "<you create this>" | |
account_key="<you create this>" | |
def create_container(abs) | |
abs.create_container(container_name) | |
end | |
def upload_blob(abs, container_name, file_name, blob_name) | |
# Upload a Blob | |
print "uploading file: #{file_name} to blob: #{blob_name}\n" | |
content = File.open(file_name, 'rb') { |file| file.read } | |
abs.create_block_blob(container_name, blob_name, content) | |
end | |
def list_containers(abs) | |
# List containers | |
abs.list_containers() | |
end | |
def list_blobs(abs, container_name) | |
# List Blobs | |
abs.list_blobs(container_name).each do |blob| | |
print "#{blob.name} - length: #{blob.properties[:content_length]}, last-modified: #{blob.properties[:last_modified]}\n" | |
end | |
end | |
def dl_blob(abs, container_name, blob_name, write_name) | |
content_length=-1 | |
# check if exists | |
abs.list_blobs(container_name).each do |blob| | |
if blob.name == blob_name | |
content_length=blob.properties[:content_length] | |
print "Target content length: #{content_length}\n" | |
end | |
end | |
if content_length == -1 | |
"Content length -1 -- aborting" | |
return | |
end | |
# if write_name exist, copy to .1? or something | |
if File.exists?(write_name) | |
File.rename(write_name, "#{write_name}.1") | |
end | |
blob_pointer=0 | |
File.open(write_name, "wb") do |f| | |
total_time=0.0 | |
while blob_pointer < content_length | |
#loop | |
# NOTE: 4MB is max size chunk azure can handle | |
blob_end_pointer = blob_pointer + CHUNK_MAX | |
#print "to block #{blob_end_pointer}\n" | |
if blob_end_pointer > content_length | |
blob_end_pointer = content_length | |
end | |
# do your work | |
t1 = Time.now | |
blob, content = abs.get_blob(container_name, blob_name) | |
t2 = Time.now | |
delta = t2 - t1 | |
total_time += delta | |
printf "--- progress %.2f percent, time: %.2f s\n", (blob_end_pointer.to_f/content_length.to_f * 100).to_f, delta | |
f.write(content) | |
blob_pointer = blob_end_pointer+1 | |
end | |
print "\nTotal dl time: %.2f seconds \n" % total_time | |
print "Avg dl rate: %.4f MBytes/sec\n" % (content_length.to_f/1024/1024/total_time.to_f) | |
end | |
end | |
def delete_blob(abs, container_name, blob_name) | |
# Delete a Blob | |
abs.delete_blob(container_name, blob_name) | |
end | |
def blob_test(bs, container_name, name, blob_name) | |
upload_blob(bs, container_name, name, blob_name ) | |
list_blobs(bs, container_name) | |
dl_blob(bs, container_name, blob_name, "#{name}.new") | |
delete_blob(bs, container_name, blob_name) | |
end | |
############################################################################# | |
Azure.configure do |config| | |
# Configure these 2 properties to use Storage | |
config.storage_account_name = storage_account | |
config.storage_access_key = account_key | |
end | |
container_name = "test-container" | |
pdf_name='programming-ruby.pdf' | |
pdf_blob_name='write-pdf' | |
# Create an azure storage blob service object | |
bs = Azure::BlobService.new | |
blob_test(bs, container_name, pdf_name, pdf_blob_name) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment