本文整理汇总了Python中azure.storage.BlobService.get_blob方法的典型用法代码示例。如果您正苦于以下问题:Python BlobService.get_blob方法的具体用法?Python BlobService.get_blob怎么用?Python BlobService.get_blob使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类azure.storage.BlobService
的用法示例。
在下文中一共展示了BlobService.get_blob方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_noargs
# 需要导入模块: from azure.storage import BlobService [as 别名]
# 或者: from azure.storage.BlobService import get_blob [as 别名]
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: test_azure_call
# 需要导入模块: from azure.storage import BlobService [as 别名]
# 或者: from azure.storage.BlobService import get_blob [as 别名]
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())
示例3: uri_get_file
# 需要导入模块: from azure.storage import BlobService [as 别名]
# 或者: from azure.storage.BlobService import get_blob [as 别名]
def uri_get_file(creds, uri, conn=None):
assert uri.startswith('wabs://')
url_tup = urlparse(uri)
if conn is None:
conn = BlobService(creds.account_name, creds.account_key,
protocol='https')
# Determin the size of the target blob
props = conn.get_blob_properties(url_tup.netloc, url_tup.path)
blob_size = int(props['content-length'])
ret_size = 0
data = ''
# WABS requires large files to be downloaded in 4MB chunks
while ret_size < blob_size:
ms_range = 'bytes={0}-{1}'.format(ret_size,
ret_size + WABS_CHUNK_SIZE - 1)
while True:
# Because we're downloading in chunks, catch rate limiting and
# connection errors here instead of letting them bubble up to the
# @retry decorator so that we don't have to start downloading the
# whole file over again.
try:
part = conn.get_blob(url_tup.netloc,
url_tup.path,
x_ms_range=ms_range)
except EnvironmentError as e:
if e.errno in (errno.EBUSY, errno.ECONNRESET):
logger.warning(
msg="retrying after encountering exception",
detail=("Exception traceback:\n{0}".format(
traceback.format_exception(*sys.exc_info()))),
hint="")
gevent.sleep(30)
else:
raise
else:
break
length = len(part)
ret_size += length
data += part
if length > 0 and length < WABS_CHUNK_SIZE:
break
elif length == 0:
break
return data
示例4: process
# 需要导入模块: from azure.storage import BlobService [as 别名]
# 或者: from azure.storage.BlobService import get_blob [as 别名]
def process(self):
account_name = self.parameters.azure_account_name
account_key = self.parameters.azure_account_key
blob_service = BlobService(account_name, account_key, protocol="https")
proxy_setting = self.parameters.https_proxy or ""
date_setting = self.parameters.date or ""
date = None
if date_setting:
if date_setting != "yesterday":
date = datetime.datetime.strptime(date_setting, "%Y-%m-%d").date() # for debbuging (probably)
elif date_setting == "yesterday":
date = datetime.date.today() - datetime.timedelta(days=1) # for normal usage
proxy_url = "https://" + proxy_setting if proxy_setting.find("https://") == -1 else proxy_setting
proxy_options = urlparse(proxy_url)
if date:
self.logger.info("Fetching for date: %s (%s)" % (date, date_setting))
else:
self.logger.info("No 'date' was specified, fetching ALL")
if proxy_options.hostname:
self.logger.info("Using https proxy(host=%s, port=%s)" % (proxy_options.hostname, proxy_options.port))
blob_service.set_proxy(host=proxy_options.hostname, port=proxy_options.port)
else:
if proxy_setting:
self.logger.info("Using NO proxy, couldn't use 'https_proxy' it was: %s" % proxy_setting)
else:
self.logger.info("Using NO proxy, 'https_proxy' was empty")
for container in blob_service.list_containers():
container_name = container.name
if container_name == "heartbeat":
continue
if date and (not container_name == "processed-" + str(date)):
self.logger.info("IGNORING container '%s' didn't match date selection" % container_name)
continue
for blob in blob_service.list_blobs(container_name):
self.logger.info("Fetching blob %s in container %s" % (container_name, blob.name))
data = blob_service.get_blob(container_name, blob.name)
cs = StringIO.StringIO(data)
report = gzip.GzipFile(fileobj=cs).read()
self.send_message(report)
示例5: while
# 需要导入模块: from azure.storage import BlobService [as 别名]
# 或者: from azure.storage.BlobService import get_blob [as 别名]
# Repeat
while(True):
# get images form *imagesQueue* - it is invoked by CRON
messages = queue_service.get_messages( imagesQueue )
if len(messages) == 0:
sleep(15)
for message in messages:
# get image: image ID
imgBlobName = b64decode( message.message_text )
print( imgBlobName )
tableRowKey = imgBlobName
try:
blob = blob_service.get_blob(blob_container, imgBlobName)
except azure.WindowsAzureMissingResourceError:
queue_service.delete_message( imagesQueue, message.message_id, message.pop_receipt )
continue
image = blobToOpenCV(blob)
# ADDED#2 ########################################
if image is None:
print "GIF attempt in analyse"
queue_service.delete_message( imagesQueue, message.message_id, message.pop_receipt )
blob_service.delete_blob( blob_container, imgBlobName)
table_service.delete_entity( tableName, tablePartitionKey, tableRowKey)
continue
示例6: AzureBlobStorage
# 需要导入模块: from azure.storage import BlobService [as 别名]
# 或者: from azure.storage.BlobService import get_blob [as 别名]
class AzureBlobStorage(Storage):
'''
classdocs
'''
def __init__(self, azure_profile):
'''
Constructor
'''
if not azure_profile:
raise Exception()
else:
container_name = azure_profile['container_name']
account_name = azure_profile['account_name']
account_key = azure_profile['key']
base_url = azure_profile['base_url']
self.blob = BlobService(
account_name=account_name, account_key=account_key)
self.container = container_name
self.base_url = base_url
def delete(self, name):
"""
Delete file.
"""
try:
self.blob.delete_blob(self.container, name)
except WindowsAzureMissingResourceError:
return False
else:
return True
def delete_files(self, files=None):
"""
Delete files in container.
"""
if not files:
files = self.listdir(self.container)[1]
for _file in files:
self.delete(_file)
def exists(self, name, with_properties=False):
"""
Existing check.
"""
result = False
blob_properties = None
try:
blob_properties = self.blob.get_blob_properties(
self.container, name)
except WindowsAzureMissingResourceError:
result = False
else:
result = True
if with_properties:
return result, blob_properties
else:
return result
def get_available_name(self, name):
return super(AzureBlobStorage, self).get_available_name(name.replace('\\', '/'))
def get_valid_name(self, name):
return name
def _list(self, path, prefix, maxresults):
result = []
blobs = self.blob.list_blobs(path, prefix, maxresults)
for _blob in blobs:
result.append(_blob.name)
return result
def listdir(self, path=None, prefix=None, maxresults=None):
"""
Catalog file list.
"""
if not path:
path = self.container
return [], self._list(path, prefix, maxresults)
def size(self, name):
"""
File size.
"""
result, properties = self.exists(name, with_properties=True)
if result:
return int(properties['content-length'])
else:
#.........这里部分代码省略.........
示例7: OptionParser
# 需要导入模块: from azure.storage import BlobService [as 别名]
# 或者: from azure.storage.BlobService import get_blob [as 别名]
parser = OptionParser()
parser.add_option("-c", "--config", metavar="FILE",
action="store", type="string", dest="conf_filename",
help="name of config file to use")
parser.add_option("-n", "--blob-name",
action="store", type="string", dest="blob_name",
help="name of the blob for Graphite path")
(opts, args) = parser.parse_args()
from azure.storage import BlobService
try:
conf_fh = open(opts.conf_filename)
conf_data = json.load(conf_fh)
# ts = TableService(conf_data["account_name"], conf_data["account_key"])
# entity = ts.get_entity(conf_data["table_name"], conf_data["partition_key"], conf_data["row_key"])
# columns = dir(entity)
# print columns
# print entity.coalitionType
bs = BlobService(conf_data["account_name"], conf_data["account_key"])
start_time = time.time()
ap_blob = bs.get_blob(conf_data["blob_container"], conf_data["blob_id"])
# print ap_blob
complete_time = time.time()
elapsed_time = complete_time - start_time
print "impresys." + opts.blob_name + ".latency\t" + str(elapsed_time) + "\t" + str(int(time.mktime(time.gmtime())))
except:
# print "Azure access failed"
print ""
示例8: AzureFS
# 需要导入模块: from azure.storage import BlobService [as 别名]
# 或者: from azure.storage.BlobService import get_blob [as 别名]
#.........这里部分代码省略.........
c_name = path[1:]
resp = self.blobs.delete_container(c_name)
if resp:
if path in self.containers:
del self.containers[path]
else:
raise FuseOSError(EACCES)
else:
raise FuseOSError(ENOSYS) # TODO support 2nd+ level mkdirs
def create(self, path, mode):
node = dict(st_mode=(S_IFREG | mode), st_size=0, st_nlink=1,
st_uid=getuid(), st_mtime=time.time())
d, f = self._parse_path(path)
if not f:
log.error("Cannot create files on root level: /")
raise FuseOSError(ENOSYS)
dir = self._get_dir(d, True)
if not dir:
raise FuseOSError(EIO)
dir['files'][f] = node
return self.open(path, data='') # reusing handler provider
def open(self, path, flags=0, data=None):
if data == None: # download contents
c_name = self.parse_container(path)
f_name = path[path.find('/', 1) + 1:]
try:
data = self.blobs.get_blob(c_name, f_name)
except WindowsAzureMissingResourceError:
dir = self._get_dir('/' + c_name, True)
if f_name in dir['files']:
del dir['files'][f_name]
raise FuseOSError(ENOENT)
except WindowsAzureError as e:
log.error("Read blob failed HTTP %d" % e.code)
raise FuseOSError(EAGAIN)
self.fd += 1
self.fds[self.fd] = (path, data, False)
return self.fd
def flush(self, path, fh=None):
if not fh:
raise FuseOSError(EIO)
else:
if fh not in self.fds:
raise FuseOSError(EIO)
path = self.fds[fh][0]
data = self.fds[fh][1]
dirty = self.fds[fh][2]
if not dirty:
return 0 # avoid redundant write
d, f = self._parse_path(path)
c_name = self.parse_container(path)
if data is None:
data = ''
示例9: BlobService
# 需要导入模块: from azure.storage import BlobService [as 别名]
# 或者: from azure.storage.BlobService import get_blob [as 别名]
sys.stderr.write("Azure key is missing")
sys.exit(1)
if header and not options.output_format:
print '\t'.join(str(h) for h in headers)
blob_service = BlobService(account_name, account_key)
for container in blob_service.list_containers():
c = container.name
if c == "heartbeat": continue
if options.date and not ( c == "processed-"+options.date ): continue
if debug: sys.stderr.write("Processing container: "+str(c)+"\n")
for b in blob_service.list_blobs(c):
if debug: sys.stderr.write("Processing blob: "+str(b.name)+"\n")
data = blob_service.get_blob(c, b.name)
cs = StringIO.StringIO(data)
gzipstream = gzip.GzipFile(fileobj=cs)
if output_format == "txt":
print gzipstream.read()
elif output_format == "json":
d = {}
i = 0
ds = gzipstream.read()
# some DCU entries contains more than 28 values (outside the
# definition of the headers)
for x in ds.strip().split("\t")[:27]:
d[headers[i]] = x
i=i+1
print (json.dumps(d, sort_keys=True))
if options.clear:
示例10: Storage
# 需要导入模块: from azure.storage import BlobService [as 别名]
# 或者: from azure.storage.BlobService import get_blob [as 别名]
class Storage(driver.Base):
supports_bytes_range = True
def __init__(self, path=None, config=None):
self._config = config
self._container = self._config.azure_storage_container
protocol = 'https' if self._config.azure_use_https else 'http'
acct_name = self._config.azure_storage_account_name
acct_key = self._config.azure_storage_account_key
self._blob = BlobService(
account_name=acct_name, account_key=acct_key, protocol=protocol)
self._init_container()
logger.debug("Initialized azureblob storage driver")
def _init_container(self):
'''Initializes image container on Azure blob storage if the container
does not exist.
'''
created = self._blob.create_container(
self._container, x_ms_blob_public_access='blob',
fail_on_exist=False)
if created:
logger.info('Created blob container for image registry.')
else:
logger.debug('Registry container already exists.')
return created
@lru.get
def get_content(self, path):
try:
return self._blob.get_blob(self._container, path)
except azure.WindowsAzureMissingResourceError:
raise exceptions.FileNotFoundError('%s is not there' % path)
@lru.set
def put_content(self, path, content):
self._blob.put_blob(self._container, path, content, 'BlockBlob')
return path
def stream_read(self, path, bytes_range=None):
try:
f = io.BytesIO()
self._blob.get_blob_to_file(self._container, path, f)
if bytes_range:
f.seek(bytes_range[0])
total_size = bytes_range[1] - bytes_range[0] + 1
else:
f.seek(0)
while True:
buf = None
if bytes_range:
# Bytes Range is enabled
buf_size = self.buffer_size
if nb_bytes + buf_size > total_size:
# We make sure we don't read out of the range
buf_size = total_size - nb_bytes
if buf_size > 0:
buf = f.read(buf_size)
nb_bytes += len(buf)
else:
# We're at the end of the range
buf = ''
else:
buf = f.read(self.buffer_size)
if not buf:
break
yield buf
except IOError:
raise exceptions.FileNotFoundError('%s is not there' % path)
def stream_write(self, path, fp):
self._blob.put_block_blob_from_file(self._container, path, fp)
def list_directory(self, path=None):
if not path.endswith('/'):
path += '/' # path=a would list a/b.txt as well as 'abc.txt'
blobs = list(self._blob.list_blobs(self._container, path))
if not blobs:
raise exceptions.FileNotFoundError('%s is not there' % path)
return [b.name for b in blobs]
def exists(self, path):
try:
self._blob.get_blob_properties(self._container, path)
return True
except azure.WindowsAzureMissingResourceError:
return False
@lru.remove
def remove(self, path):
is_blob = self.exists(path)
#.........这里部分代码省略.........
示例11: retrieve
# 需要导入模块: from azure.storage import BlobService [as 别名]
# 或者: from azure.storage.BlobService import get_blob [as 别名]
def retrieve(entity, entity_id):
blob_service = BlobService(account_name='shnergledata',
account_key=os.environ['BLOB_KEY'])
name = '/' + entity + '/' + entity_id
blob = blob_service.get_blob('images', name)
return blob