本文整理汇总了Python中azure.storage.BlobService类的典型用法代码示例。如果您正苦于以下问题:Python BlobService类的具体用法?Python BlobService怎么用?Python BlobService使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BlobService类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_noargs
def handle_noargs(self, **options):
try:
blob_service = BlobService(AZURE_ACCOUNT_NAME, AZURE_ACCOUNT_KEY)
mixes = Mix.objects.filter(archive_updated=False)
c = len(mixes)
i = 1
for mix in mixes:
try:
blob_name = "%s.%s" % (mix.uid, mix.filetype)
blob = blob_service.get_blob(AZURE_CONTAINER, blob_name)
if blob:
download_name = smart_str('Deep South Sounds - %s.%s' %
(mix.title, mix.filetype))
blob_service.set_blob_properties(
AZURE_CONTAINER,
blob_name,
x_ms_blob_content_type='application/octet-stream',
x_ms_blob_content_disposition='attachment;filename="%s"' % (download_name)
)
print "Processed: %s (%d of %d)" % (mix.uid, i, c)
i = i + 1
mix.archive_updated = True
mix.save()
else:
print "No blob found for: %s" % mix.uid
except WindowsAzureMissingResourceError:
print "No blob found for: %s" % mix.uid
except Exception, ex:
print "Error processing blob %s: %s" % (mix.uid, ex.message)
except Exception, ex:
print "Fatal error, bailing. %s" % (ex.message)
示例2: download_azure_blob
def download_azure_blob(account_name, account_key, file_uri, download_dir):
(blob_name, container_name) = parse_blob_uri(file_uri)
host_base = get_host_base_from_uri(file_uri)
download_path = os.path.join(download_dir, blob_name)
blob_service = BlobService(account_name, account_key, host_base=host_base)
max_retry = 3
for retry in range(1, max_retry + 1):
try:
blob_service.get_blob_to_path(container_name, blob_name, download_path)
except Exception:
hutil.error('Failed to download Azure blob, retry = ' + str(retry) + ', max_retry = ' + str(max_retry))
if retry != max_retry:
hutil.log('Sleep 10 seconds')
time.sleep(10)
else:
waagent.AddExtensionEvent(name=ExtensionShortName,
op=Operation.Download,
isSuccess=False,
message="(03303)Failed to download file from Azure Storage")
raise Exception('Failed to download azure blob: ' + blob_name)
waagent.AddExtensionEvent(name=ExtensionShortName,
op=Operation.Download,
isSuccess=True,
message="(03301)Succeeded to download file from Azure Storage")
return download_path
示例3: test_azure_call
def test_azure_call(request):
import os
try:
from azure.storage import BlobService
bs = BlobService(os.environ["AZURE_STORAGE_ACCOUNT"], os.environ["AZURE_STORAGE_ACCESS_KEY"])
import random
container_name = hex(int(random.random() * 1000000000))
bs.create_container(container_name)
bs.put_blob(container_name, 'testblob', 'hello world\n', 'BlockBlob')
blob = bs.get_blob(container_name, 'testblob')
if blob != 'hello world\n':
return HttpResponse("Failed!", status = '404')
bs.delete_blob(container_name, 'testblob')
bs.delete_container(container_name)
return HttpResponse("Succeeded!")
except:
try:
import traceback
return HttpResponse(traceback.format_exc() + str(os.environ.keys()))
except:
import traceback
return HttpResponse(traceback.format_exc())
示例4: deprovision
def deprovision(instance_id):
"""
Deprovision an existing instance of this service
DELETE /v2/service_instances/<instance_id>:
<instance_id> is the Cloud Controller provided
value used to provision the instance
return:
As of API 2.3, an empty JSON document
is expected
"""
global subscription_id
global cert
global account_name
global account_key
if account_name and account_key:
blob_service = BlobService(account_name, account_key)
container_name = '{0}-{1}'.format(CONTAINER_NAME_PREFIX, instance_id)
blob_service.delete_container(container_name)
if account_name.startswith(STORAGE_ACCOUNT_NAME_PREFIX):
sms = ServiceManagementService(subscription_id, cert_file)
sms.delete_storage_account(account_name)
return jsonify({})
示例5: delete
def delete(self):
properties = self.__SMS.get_deployment_by_name(self.name, self.name)
media_link = properties.role_list.roles[0].os_virtual_hard_disk.media_link
storage_name = media_link[media_link.find("//") + 2:media_link.find(".blob")]
from Azure.AzureVolumes.AzureVolumes import AzureVolumescls
volume_service = AzureVolumescls(credentials=self._credentials)
volumes = volume_service.list_volumes()
volume_to_be_deleted = None
for volume in volumes:
if volume.instance_id == self.name:
volume_to_be_deleted = volume
break
self.__SMS.delete_deployment(self.name, self.name)
self.__SMS.delete_hosted_service(self.name)
volume_to_be_deleted.delete()
# delete image from storge
from azure.storage import BlobService
keys = self.__SMS.get_storage_account_keys(storage_name)
blob_service = BlobService(account_name=storage_name, account_key=keys.storage_service_keys.primary)
blob_service.delete_container(self.name, fail_not_exist=True)
示例6: make_blob_sas_url
def make_blob_sas_url(account_name,
account_key,
container_name,
blob_name,
permission='w',
duration=16):
"""
Generate a Blob SAS URL to allow a client to upload a file.
account_name: Storage account name.
account_key: Storage account key.
container_name: Storage container.
blob_name: Blob name.
duration: A timedelta representing duration until SAS expiration.
SAS start date will be utcnow() minus one minute. Expiry date
is start date plus duration.
Returns the SAS URL.
"""
sas = SharedAccessSignature(account_name, account_key)
resource_path = '%s/%s' % (container_name, blob_name)
date_format = "%Y-%m-%dT%H:%M:%SZ"
start = datetime.datetime.utcnow() - datetime.timedelta(minutes=5)
expiry = start + datetime.timedelta(minutes=duration)
sap = SharedAccessPolicy(AccessPolicy(
start.strftime(date_format),
expiry.strftime(date_format),
permission))
sas_token = sas.generate_signed_query_string(resource_path, 'b', sap)
blob_url = BlobService(account_name, account_key)
url = blob_url.make_blob_url(container_name=container_name, blob_name=blob_name, sas_token=sas_token)
return url
示例7: store
def store(image, entity, entity_id):
blob_service = BlobService(account_name='shnergledata',
account_key=os.environ['BLOB_KEY'])
myblob = image.read()
name = '/' + entity + '/' + entity_id
blob_service.put_blob('images', name, myblob, x_ms_blob_type='BlockBlob')
return True
示例8: download_azure_blob
def download_azure_blob(account_name, account_key, file_uri, download_dir):
waagent.AddExtensionEvent(name=ExtensionShortName, op="EnableInProgress", isSuccess=True, message="Downloading from azure blob")
try:
(blob_name, container_name) = parse_blob_uri(file_uri)
host_base = get_host_base_from_uri(file_uri)
download_path = os.path.join(download_dir, blob_name)
blob_service = BlobService(account_name, account_key, host_base=host_base)
except Exception as e:
waagent.AddExtensionEvent(name=ExtensionShortName, op='DownloadInProgress', isSuccess=True, message='Enable failed with the azure storage error : {0}, stack trace: {1}'.format(str(e), traceback.format_exc()))
hutil.error('Failed to enable the extension with error: %s, stack trace: %s' %(str(e), traceback.format_exc()))
hutil.do_exit(1, 'Enable', 'error', '1', 'Enable failed: {0}'.format(e))
max_retry = 3
for retry in range(1, max_retry + 1):
try:
blob_service.get_blob_to_path(container_name, blob_name, download_path)
except Exception:
hutil.error('Failed to download Azure blob, retry = ' + str(retry) + ', max_retry = ' + str(max_retry))
if retry != max_retry:
hutil.log('Sleep 10 seconds')
time.sleep(10)
else:
waagent.AddExtensionEvent(name=ExtensionShortName,
op=Operation.Download,
isSuccess=False,
message="(03303)Failed to download file from Azure Storage")
raise Exception('Failed to download azure blob: ' + blob_name)
waagent.AddExtensionEvent(name=ExtensionShortName,
op=Operation.Download,
isSuccess=True,
message="(03301)Succeeded to download file from Azure Storage")
return download_path
示例9: resizeimageandputinazure
def resizeimageandputinazure (strkey, url):
maxwidthandheight = 150
resize = False
bytes = urllib2.urlopen(url).read()
img = Image.open( io.BytesIO (bytes))
newwidth = img.width
newheight = img.height
if (newheight > newwidth and newheight > maxwidthandheight):
heightpercent = maxwidthandheight/float(newheight)
newheight = maxwidthandheight
newwidth = int((float(img.width)*float(heightpercent)))
resize = True
elif (newwidth > newheight and newwidth > maxwidthandheight):
widthpercent = maxwidthandheight/float(newwidth)
newwidth = maxwidthandheight
newheight = int((float(img.height)*float(widthpercent)))
resize = True
if resize:
newimg = img.resize((newwidth, newheight), Image.ANTIALIAS)
newimg.format = img.format
newio = io.BytesIO()
newimg.save (newio, 'JPEG')
bytes = newio.getvalue()
blob_service = BlobService(account_name='wanderight', account_key='gdmZeJOCx3HYlFPZZukUhHAfeGAu4cfHWGQZc3+HIpkBHjlznUDjhXMl5HWh5MgbjpJF09ZxRaET1JVF9S2MWQ==')
blob_service.put_block_blob_from_bytes(config['container'], 'images/' + strkey, bytes,
x_ms_blob_content_type='image/jpg', x_ms_meta_name_values={'url':url})
示例10: submit
def submit():
blob_service = BlobService(account_name=ACCOUNT_NAME, account_key=ACCOUNT_KEY)
# Get a SAS signature (read for 24 hours) for the input container save to a string
inputsig = sasUrl(account=ACCOUNT_NAME, key=ACCOUNT_KEY, container=INPUT_CONTAINER, permission='r')
# Get a SAS signature (write for 24 hours) for the output container save to a string
outputsig = sasUrl(account = ACCOUNT_NAME, key = ACCOUNT_KEY, container = OUTPUT_CONTAINER, permission = 'rwl')
# List all the blobs and dump the content to a string
blobs = blob_service.list_blobs(INPUT_CONTAINER)
bloblist = []
for blob in blobs:
bloblist.append(blob.name)
os.environ[SLURMDEMO_INPUTSIG] = inputsig
os.environ[SLURMDEMO_OUTPUTSIG] = outputsig
os.environ[SLURMDEMO_BLOBLIST] = json.dumps(bloblist)
os.environ[SLURMDEMO_INPUTCONTAINER] = INPUT_CONTAINER
os.environ[SLURMDEMO_OUTPUTCONTAINER] = OUTPUT_CONTAINER
os.environ[SLURMDEMO_INPUTACCOUNT] = ACCOUNT_NAME
os.environ[SLURMDEMO_OUTPUTACCOUNT] = ACCOUNT_NAME
# Call sbatch
cli = "sbatch --array=0-{nb} slurmdemo.sh".format(nb=len(bloblist))
run(cli, showoutput=True)
示例11: remove_image
def remove_image(album_name, username):
gallery_db = connect_to_db()
albums = gallery_db.albums
requested_album = albums.find_one({"name": album_name})
if not requested_album:
return redirect(url_for('albums', album =album_name, message="album not found"))
if not username in requested_album["write"]:
return redirect(url_for('albums', album = album_name, message="permission denied"))
image = request.form.get('image', '')
if not image:
return redirect(url_for('albums', album=album_name, message="no image was chosen for removal"))
blob_service = BlobService(account_name=ACCOUNT_NAME, account_key=ACCOUNT_KEY)
try:
blob_service.delete_blob(CONTAINER_NAME, image)
except WindowsAzureMissingResourceError:
# Even if the file is not in the blob storage, we want to remove it from the album
pass
gallery_db.albums.update({'name': album_name}, {'$pull': {'images': image}})
# increment the counter of the removed images
stats_client.incr("images removed", 1)
return redirect(url_for('albums', album=album_name))
示例12: add_image
def add_image(album_name, username):
gallery_db = connect_to_db()
albums = gallery_db.albums
requested_album = albums.find_one({"name": album_name})
if not requested_album:
return redirect(url_for('albums', album =album_name, message="album not found"))
if not username in requested_album["write"]:
return redirect(url_for('albums', album = album_name, message="permission denied"))
if 'image[]' not in request.files:
return redirect(url_for('albums', album = album_name, message="no file uploaded"))
for req_file in request.files.getlist('image[]'):
file_name = uuid.uuid4().hex
stats_upload_timer = stats_client.timer("upload timer")
stats_upload_timer.start()
blob_service = BlobService(account_name=ACCOUNT_NAME, account_key=ACCOUNT_KEY)
blob_service.put_block_blob_from_file(CONTAINER_NAME, file_name, req_file.stream)
gallery_db.albums.update({'name': album_name}, {'$push': {'images': file_name}})
stats_upload_timer.stop()
# increment the counter of the uploaded images
stats_client.incr("images uploaded", len(request.files.getlist('image[]')))
return redirect(url_for('albums', album = album_name))
示例13: upload_documents
def upload_documents():
data = request.json.get('data', None)
if not data:
return jsonify(status=400, message='No file content passed')
data = data.decode("base64")
upload_handler = get_upload_handler()
# force is a flag that signals to upload the current file even if it was uploaded before
force = request.json.get('force', None)
if force is None or force.lower() != "true":
if upload_handler.is_file_already_uploaded(data, current_user.get_id()):
return jsonify(status=400, message='File content was already uploaded. Force upload by adding the force boolean')
blob_service = BlobService(account_name=BLOB_ACCOUNT_NAME, account_key=BLOB_ACCOUNT_KEY)
filename = uuid.uuid4().hex
# put the data in the container using a random filename
blob_service.put_block_blob_from_bytes(BLOB_CONTAINER_NAME, filename, data)
task_collection = get_db().task_collection
# update the task db with the new task (which is parsing the new data file)
task_id = upload_handler.update_uploaded_file(filename, data, current_user.get_id())
return jsonify(status=200, message='Task created successfully', task_id=task_id)
示例14: get_image
def get_image(album_name, image_name, username):
gallery_db = connect_to_db()
albums = gallery_db.albums
requested_album = albums.find_one({"name": album_name})
if not requested_album:
return redirect(url_for('static', filename='image_not_found.gif'))
if not (username in requested_album["write"] or username in requested_album["read"]):
return redirect(url_for('static', filename='image_not_found.gif'))
if image_name not in requested_album["images"]:
return redirect(url_for('static', filename='image_not_found.gif'))
try:
stats_download_timer = stats_client.timer("download timer")
# start to time the download
stats_download_timer.start()
blob_service = BlobService(account_name=ACCOUNT_NAME, account_key=ACCOUNT_KEY)
data = blob_service.get_blob_to_bytes(CONTAINER_NAME, image_name)
response = make_response(data)
response.headers["Content-Disposition"] = "filename=%s.jpg" % image_name
response.headers['Content-type'] = 'image/jpeg'
stats_download_timer.stop()
stats_client.incr("images downloaded", 1)
return response
except Exception as ex:
# TODO: different image in this case?
stats_download_timer.stop()
return redirect(url_for('static', filename='image_not_found.gif'))
示例15: AzureTransfer
class AzureTransfer(BaseTransfer):
def __init__(self, account_name, account_key, container_name):
BaseTransfer.__init__(self)
self.account_name = account_name
self.account_key = account_key
self.container_name = container_name
self.conn = BlobService(account_name=self.account_name, account_key=self.account_key)
self.container = self.get_or_create_container(self.container_name)
self.log.debug("AzureTransfer initialized")
def get_metadata_for_key(self, key):
key = fix_path(key)
return self.list_path(key)[0]['metadata']
def list_path(self, path):
return_list = []
path = fix_path(path)
self.log.info("Asking for listing of: %r", path)
for r in self.conn.list_blobs(self.container_name, prefix=path, delimiter="/",
include="metadata"):
entry = {"name": r.name, "size": r.properties.content_length,
"last_modified": dateutil.parser.parse(r.properties.last_modified),
"metadata": r.metadata}
return_list.append(entry)
return return_list
def delete_key(self, key_name):
key_name = fix_path(key_name)
self.log.debug("Deleting key: %r", key_name)
return self.conn.delete_blob(self.container_name, key_name)
def get_contents_to_file(self, obj_key, filepath_to_store_to):
obj_key = fix_path(obj_key)
self.log.debug("Starting to fetch the contents of: %r to: %r", obj_key, filepath_to_store_to)
return self.conn.get_blob_to_path(self.container_name, obj_key, filepath_to_store_to)
def get_contents_to_string(self, obj_key):
obj_key = fix_path(obj_key)
self.log.debug("Starting to fetch the contents of: %r", obj_key)
return self.conn.get_blob_to_bytes(self.container_name, obj_key), self.get_metadata_for_key(obj_key)
def store_file_from_memory(self, key, memstring, metadata=None):
# For whatever reason Azure requires all values to be strings at the point of sending
metadata_to_send = dict((str(k), str(v)) for k, v in metadata.items())
self.conn.put_block_blob_from_bytes(self.container_name, key, memstring,
x_ms_meta_name_values=metadata_to_send)
def store_file_from_disk(self, key, filepath, metadata=None):
# For whatever reason Azure requires all values to be strings at the point of sending
metadata_to_send = dict((str(k), str(v)) for k, v in metadata.items())
self.conn.put_block_blob_from_path(self.container_name, key, filepath,
x_ms_meta_name_values=metadata_to_send)
def get_or_create_container(self, container_name):
start_time = time.time()
self.conn.create_container(container_name)
self.log.debug("Got/Created container: %r successfully, took: %.3fs", container_name, time.time() - start_time)
return container_name