class FileService(Component): def __init__(self): self.blob_service = None def generate_blob_service(self): if self.blob_service is None: # if storage info doesn't exist in config.py upload file function stop working self.blob_service = BlobService(account_name=self.util.get_config("storage.azure.account_name"), account_key=self.util.get_config("storage.azure.account_key"), host_base=self.util.get_config("storage.azure.blob_service_host_base")) def create_container_in_storage(self, container_name, access): """ create a container if doesn't exist :param container_name: :param access: :return: """ self.generate_blob_service() try: names = map(lambda x: x.name, self.blob_service.list_containers()) if container_name not in names: self.blob_service.create_container(container_name, x_ms_blob_public_access=access) else: self.log.debug("container already exsit in storage") return True except Exception as e: self.log.error(e) return False def upload_file_to_azure(self, stream, container_name, blob_name): try: if self.create_container_in_storage(container_name, 'container'): self.blob_service.put_block_blob_from_file(container_name, blob_name, stream) return self.blob_service.make_blob_url(container_name, blob_name) else: return None except Exception as e: self.log.error(e) return None def upload_file_to_azure_from_path(self, path, container_name, blob_name): try: if self.create_container_in_storage(container_name, 'container'): self.blob_service.put_block_blob_from_path(container_name, blob_name, path) return self.blob_service.make_blob_url(container_name, blob_name) else: return None except Exception as e: self.log.error(e) return None def delete_file_from_azure(self, container_name, blob_name): try: if self.create_container_in_storage(container_name, 'container'): self.blob_service.delete_blob(container_name, blob_name) except Exception as e: self.log.error(e) return None
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)
def download(): blob_service = BlobService(account_name='squadshots', account_key='UgxaWKAKv2ZvhHrPt0IHi4EQedPpZw35r+RXkAYB2eICPrG3TjSwk2G8gUzG/PNDDTV+4CVCYWCvZSiad5xMQQ==') try: blob_service.get_blob_to_path('album','image','static/output.png') except Exception as e: print e blobs = blob_service.list_blobs('album',None,None,None,'metadata',None) for blob in blobs: if blob.metadata != None: for key in blob.metadata: if (blob.metadata)[key] == session['username']: blob_service.get_blob_to_path('album',blob.name,'static/output.png') for i in blob_service.list_containers(): print "This container is " + i.name return render_template('album.html',filename="static/output.png")
class AzureFS(LoggingMixIn, Operations): """Azure Blob Storage filesystem""" blobs = None containers = dict() # <cname, dict(stat:dict, #files:None|dict<fname, stat>) fds = dict() # <fd, (path, bytes, dirty)> fd = 0 def __init__(self, account, key): self.blobs = BlobService(account, key) self.rebuild_container_list() def convert_to_epoch(self, date): """Converts Tue, 31 Jul 2012 07:17:34 GMT format to epoch""" return int(time.mktime(time.strptime(date, TIME_FORMAT))) def rebuild_container_list(self): cmap = dict() cnames = set() for c in self.blobs.list_containers(): date = c.properties.last_modified cstat = dict(st_mode=(S_IFDIR | 0755), st_uid=getuid(), st_size=0, st_mtime=self.convert_to_epoch(date)) cname = c.name cmap['/' + cname] = dict(stat=cstat, files=None) cnames.add(cname) cmap['/'] = dict(files={}, stat=dict(st_mode=(S_IFDIR | 0755), st_uid=getuid(), st_size=0, st_mtime=int(time.time()))) self.containers = cmap # destroys fs tree cache resistant to misses def _parse_path(self, path): # returns </dir, file(=None)> if path.count('/') > 1: # file return str(path[:path.rfind('/')]), str(path[path.rfind('/') + 1:]) else: # dir pos = path.rfind('/', 1) if pos == -1: return path, None else: return str(path[:pos]), None def parse_container(self, path): base_container = path[1:] # /abc/def/g --> abc if base_container.find('/') > -1: base_container = base_container[:base_container.find('/')] return str(base_container) def _get_dir(self, path, contents_required=False): if not self.containers: self.rebuild_container_list() if path in self.containers and not (contents_required and \ self.containers[path]['files'] is None): return self.containers[path] cname = self.parse_container(path) if '/' + cname not in self.containers: raise FuseOSError(ENOENT) else: if self.containers['/' + cname]['files'] is None: # fetch contents of container log.info("------> CONTENTS NOT FOUND: %s" % cname) blobs = self.blobs.list_blobs(cname) dirstat = dict(st_mode=(S_IFDIR | 0755), st_size=0, st_uid=getuid(), st_mtime=time.time()) if self.containers['/' + cname]['files'] is None: self.containers['/' + cname]['files'] = dict() for f in blobs: blob_name = f.name blob_date = f.properties.last_modified blob_size = long(f.properties.content_length) node = dict(st_mode=(S_IFREG | 0644), st_size=blob_size, st_mtime=self.convert_to_epoch(blob_date), st_uid=getuid()) if blob_name.find('/') == -1: # file just under container self.containers['/' + cname]['files'][blob_name] = node return self.containers['/' + cname] return None def _get_file(self, path): d, f = self._parse_path(path) dir = self._get_dir(d, True) if dir is not None and f in dir['files']: return dir['files'][f] def getattr(self, path, fh=None): d, f = self._parse_path(path) if f is None: dir = self._get_dir(d) return dir['stat'] else: file = self._get_file(path) if file: return file raise FuseOSError(ENOENT) # FUSE def mkdir(self, path, mode): if path.count('/') <= 1: # create on root name = path[1:] if not 3 <= len(name) <= 63: log.error("Container names can be 3 through 63 chars long.") raise FuseOSError(ENAMETOOLONG) if name is not name.lower(): log.error("Container names cannot contain uppercase \ characters.") raise FuseOSError(EACCES) if name.count('--') > 0: log.error('Container names cannot contain consecutive \ dashes (-).') raise FuseOSError(EAGAIN) #TODO handle all "-"s must be preceded by letter or numbers #TODO starts with only letter or number, can contain letter, nr,'-' resp = self.blobs.create_container(name) if resp: self.rebuild_container_list() log.info("CONTAINER %s CREATED" % name) else: raise FuseOSError(EACCES) log.error("Invalid container name or container already \ exists.") else: raise FuseOSError(ENOSYS) # TODO support 2nd+ level mkdirs def rmdir(self, path): if path.count('/') == 1: 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 = '' try: if len(data) < 64 * 1024 * 1024: # 64 mb self.blobs.put_blob(c_name, f, data, 'BlockBlob') else: # divide file by blocks and upload block_size = 8 * 1024 * 1024 num_blocks = int(math.ceil(len(data) * 1.0 / block_size)) rd = str(random.randint(1, 1e8)) block_ids = list() for i in range(num_blocks): part = data[i * block_size:min((i + 1) * block_size, len(data))] block_id = base64.encodestring('%s_%s' % (rd, (8 - len(str(i))) * '0' + str(i))) self.blobs.put_block(c_name, f, part, block_id) block_ids.append(block_id) self.blobs.put_block_list(c_name, f, block_ids) except WindowsAzureError: raise FuseOSError(EAGAIN) dir = self._get_dir(d, True) if not dir or f not in dir['files']: raise FuseOSError(EIO) # update local data dir['files'][f]['st_size'] = len(data) dir['files'][f]['st_mtime'] = time.time() self.fds[fh] = (path, data, False) # mark as not dirty return 0 def release(self, path, fh=None): if fh is not None and fh in self.fds: del self.fds[fh] def truncate(self, path, length, fh=None): return 0 # assume done, no need def write(self, path, data, offset, fh=None): if not fh or fh not in self.fds: raise FuseOSError(ENOENT) else: d = self.fds[fh][1] if d is None: d = "" self.fds[fh] = (self.fds[fh][0], d[:offset] + data, True) return len(data) def unlink(self, path): c_name = self.parse_container(path) d, f = self._parse_path(path) try: self.blobs.delete_blob(c_name, f) _dir = self._get_dir(path, True) if _dir and f in _dir['files']: del _dir['files'][f] return 0 except WindowsAzureMissingResourceError: raise FuseOSError(ENOENT) except Exception as e: raise FuseOSError(EAGAIN) def readdir(self, path, fh): if path == '/': return ['.', '..'] + [x[1:] for x in self.containers.keys() \ if x is not '/'] dir = self._get_dir(path, True) if not dir: raise FuseOSError(ENOENT) return ['.', '..'] + dir['files'].keys() def read(self, path, size, offset, fh): if not fh or fh not in self.fds: raise FuseOSError(ENOENT) f_name = path[path.find('/', 1) + 1:] c_name = path[1:path.find('/', 1)] try: data = self.blobs.get_blob(c_name, f_name) self.fds[fh] = (self.fds[fh][0], data, False) return data[offset:offset + size] except URLError, e: if e.code == 404: raise FuseOSError(ENOENT) elif e.code == 403: raise FUSEOSError(EPERM) else: log.error("Read blob failed HTTP %d" % e.code) raise FuseOSError(EAGAIN) data = self.fds[fh][1] if data is None: data = "" return data[offset:offset + size]
class SAzure(SyncStorage): def __init__(self): super().__init__() self.msg_key_na = _('Key not available') try: import alxlib.key key = alxlib.key.Key() if os.path.isfile(key.get_path()): sys.path.insert(0, key.get_dir()) import alxkey self.key = alxkey.alxkey_azure """self.blob = BlobService(account_name=self.key['AZURE_STORAGE_ACCOUNT_NAME'], account_key=self.key['AZURE_ACCESS_KEY'])""" else: # raise (self.msg_key_na) self.key = None except: pass # raise (self.msg_key_na) def connect(self): try: self.blob = BlobService(account_name=self.key['AZURE_STORAGE_ACCOUNT_NAME'], account_key=self.key['AZURE_ACCESS_KEY']) return self.blob.list_containers(maxresults=1) except: return None def connect_blob(self, az_account_name=None, az_account_key=None): try: if az_account_name != None: self.key['AZURE_STORAGE_ACCOUNT_NAME'] = az_account_name self.key['AZURE_ACCESS_KEY'] = az_account_key return self.connect() except: return None def path_clean(self, path: str): try: i = path.index("//") + 2 self.container = path[0:i] if path[len(path) - 1] != "/": path += "/" return path[i:] except: print(_("Bad Path")) exit(1) def spath(self, container, root, b): spath = SyncPath() spath.BasePath = container if b.name[len(b.name)-1]=="/": spath.IsDir= True else: spath.IsFile= True spath.AbsPath = b.name if len(root)>0: spath.SPath = b.name[len(root) - 1:] else: spath.SPath=b.name spath.Size = b.properties.content_length import alxlib.time_help spath.ModifiedTS = alxlib.time_help.to_timestamp(b.properties.last_modified) spath.MD5 = b.properties.content_md5 spath.sys="azure" return spath def path_split(self, path: str): try: list = path.split("/") container = list[0] uri = "" if len(list) > 1: uri = "/".join(map(str, list[1:])) return container, uri except: print(_("Bad path")) exit(1) def path_list_blobs(self, container, uri): try: if len(uri)>0: blobs = self.blob.list_blobs(container, prefix=uri) else: blobs = self.blob.list_blobs(container) """for blob in blobs: print(blob.properties.__dict__) print(blob.name) print(blob.url)""" return blobs except Exception as e: print(_("Bad connection")) logging.warning("container {0}, path {1}".format(container, uri)) exit(1) def path_list(self, path): try: logging.debug("path_list {0}".format(path)) container, uri = self.path_split(path) logging.debug("Container: {0}, Uri: {1}".format(container, uri)) self.connect() self.blob.create_container(container) blobs = self.path_list_blobs(container, uri) d = {} for b in blobs: spath = self.spath(container, uri, b) # print(b.__dict__) #print(str(b.properties.last_modified.__dict__)) #print(str(spath.ModifiedTS)) d[spath.SPath] = spath # print(d) return d except Exception as e: print(e) def remove(self, src: SyncPath): try: logging.debug("Removing {0}".format(src.AbsPath)) self.connect() self.blob.create_container(src.BasePath) self.blob.delete_blob(src.BasePath, src.AbsPath) except: pass def copy_local2azure(self, src, base_dir): try: container, uri = self.path_split(base_dir) if len(src.SPath)>0 and src.SPath[0]=="/": path= uri+ src.SPath[1:] else: path= uri+src.SPath logging.debug("copy_local2azure Spath {0}. path:{1}".format(src.SPath, path)) self.connect() if not src.IsDir: self.blob.put_block_blob_from_path (container, path, src.AbsPath) else: self.blob.put_block_blob_from_text(container, path+"/", "") except Exception as e: print("Error Copying") print(e) def copy_azure2local(self, src, base_dir): try: if len(src.SPath)>0 and (src.SPath[0] == "/" or src.SPath[0] == "\\") : path = src.SPath[1:] else: path = src.SPath path= os.path.normpath(os.path.join(base_dir, path)) logging.debug("copy_azure2local basedir:{0} Spath {1}, path {2}, abs: {3}".format( base_dir, src.SPath, path, src.AbsPath)) if not os.path.isdir(path): os.makedirs(os.path.dirname(path), exist_ok=True) #print( os.path.dirname(path)+"***************") if not (len(src.AbsPath)>0 and src.AbsPath[len(src.AbsPath)-1]=="/"): self.blob.get_blob_to_path(src.BasePath, src.AbsPath, path) """container, uri = self.path_split(base_dir) if len(src.SPath)>0 and src.SPath[0]=="/": path= uri+ src.SPath[1:] else: path= uri+src.SPath self.connect() if not src.IsDir: self.blob.get_blob_to_path(src.BasePath, path, src.AbsPath) else: self.blob.put_block_blob_from_text(container, path, "")""" except Exception as e: print("Error copying") print(e)
class BlobServiceAdapter(Component): """The :class:`BlobServiceAdapter` class is a thin wrapper over azure.storage.BlobService. All the attributes of the wrapper stream are proxied by the adapter so it's possible to do ``adapter.create_container()`` instead of the long form ``adapter.blob_service.adapter()``. """ def __init__(self): self.blob_service = BlobService( account_name=self.util.get_config("storage.azure.account_name"), account_key=self.util.get_config("storage.azure.account_key"), host_base=self.util.get_config("storage.azure.blob_service_host_base"), ) def __getattr__(self, name): return getattr(self.blob_service, name) def create_container_in_storage(self, container_name, access="container"): """create a container if doesn't exist :type container_name: str|unicode :param container_name: Name of container to create. :type access: str|unicode :param access: Optional. Possible values include: container, blob :return: """ try: names = [x.name for x in self.blob_service.list_containers()] if container_name not in names: return self.blob_service.create_container(container_name, x_ms_blob_public_access=access) else: self.log.debug("container already exists in storage") return True except Exception as e: self.log.error(e) return False def upload_file_to_azure(self, container_name, blob_name, stream): """ Creates a new block blob from a file/stream, or updates the content of an existing block blob, with automatic chunking and progress notifications. :type container_name: str|unicode :param container_name: Name of existing container. :type blob_name: str | unicode :param blob_name: Name of blob to create or update. :type stream: file :param stream: Opened file/stream to upload as the blob content. """ try: if self.create_container_in_storage(container_name, "container"): self.blob_service.put_block_blob_from_file(container_name, blob_name, stream) return self.blob_service.make_blob_url(container_name, blob_name) else: return None except Exception as e: self.log.error(e) return None def upload_file_to_azure_from_bytes(self, container_name, blob_name, blob): """ Creates a new block blob from an array of bytes, or updates the content of an existing block blob, with automatic chunking and progress notifications. :type container_name: str|unicode :param container_name: Name of existing container. :type blob_name: str|unicode :param blob_name: Name of blob to create or update. :type blob: bytes :param blob: Content of blob as an array of bytes. """ try: if self.create_container_in_storage(container_name, "container"): self.blob_service.put_block_blob_from_bytes(container_name, blob_name, blob) return self.blob_service.make_blob_url(container_name, blob_name) else: return None except Exception as e: self.log.error(e) return None def upload_file_to_azure_from_text(self, container_name, blob_name, text): """ Creates a new block blob from str/unicode, or updates the content of an existing block blob, with automatic chunking and progress notifications. :type container_name: str|unicode :param container_name: Name of existing container. :type blob_name: str|unicode :param blob_name: Name of blob to create or update. :type text: str|unicode :param text: Text to upload to the blob. """ try: if self.create_container_in_storage(container_name, "container"): self.blob_service.put_block_blob_from_text(container_name, blob_name, text) return self.blob_service.make_blob_url(container_name, blob_name) else: return None except Exception as e: self.log.error(e) return None def upload_file_to_azure_from_path(self, container_name, blob_name, path): """ Creates a new page blob from a file path, or updates the content of an existing page blob, with automatic chunking and progress notifications. :type container_name: str|unicode :param container_name: Name of existing container. :type blob_name: str|unicode :param blob_name: Name of blob to create or update. :type path: str|unicode :param path: Path of the file to upload as the blob content. """ try: if self.create_container_in_storage(container_name, "container"): self.blob_service.put_block_blob_from_path(container_name, blob_name, path) return self.blob_service.make_blob_url(container_name, blob_name) else: return None except Exception as e: self.log.error(e) return None def delete_file_from_azure(self, container_name, blob_name): try: if self.create_container_in_storage(container_name, "container"): self.blob_service.delete_blob(container_name, blob_name) except Exception as e: self.log.error(e) return None
osHardDiskName = sys.argv[6] #-------------- # Azureサービスオブジェクトを作成 sms = ServiceManagementService(subscription, certPath) blobService = BlobService(storageAccount, accessKey) #-------------- # コンテナとBlobオブジェクトを取得 # mediaLinkからBlobオブジェクトを得る logger.debug("deleteOSandDataDisk.py: Container and Blob object get mediaLink...(%s)" % mediaLink) # # 消すべきBlobの存在チェック # # コンテナ一覧を取得 containerList = blobService.list_containers() targetBlob = None for container in containerList: # # コンテナに含まれるBlob一覧を取得 blobList = blobService.list_blobs(container.name) for blob in blobList: # # URIから、先頭のhttp*://を取り除いた文字列を比較 blobname = blob.url.split('://')[1] if blobname == mediaLink.split('://')[1]: logger.debug('deleteOSandDataDisk.py: find target blobname: ' + blobname) targetBlob = blob targetContainer = container # # 見つからなければエラー終了 if (targetBlob is None):
else: sys.stderr.write("Azure account name is missing") sys.exit(1) if options.account_key: account_key=options.account_key else: 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()
class FileService(Component): def __init__(self): self.blob_service = None def generate_blob_service(self): if self.blob_service is None: # if storage info doesn't exist in config.py upload file function stop working self.blob_service = BlobService( account_name=self.util.get_config( "storage.azure.account_name"), account_key=self.util.get_config("storage.azure.account_key"), host_base=self.util.get_config( "storage.azure.blob_service_host_base")) def create_container_in_storage(self, container_name, access): """ create a container if doesn't exist :param container_name: :param access: :return: """ self.generate_blob_service() try: names = map(lambda x: x.name, self.blob_service.list_containers()) if container_name not in names: self.blob_service.create_container( container_name, x_ms_blob_public_access=access) else: self.log.debug("container already exsit in storage") return True except Exception as e: self.log.error(e) return False def upload_file_to_azure(self, stream, container_name, blob_name): try: if self.create_container_in_storage(container_name, 'container'): self.blob_service.put_block_blob_from_file( container_name, blob_name, stream) return self.blob_service.make_blob_url(container_name, blob_name) else: return None except Exception as e: self.log.error(e) return None def upload_file_to_azure_from_path(self, path, container_name, blob_name): try: if self.create_container_in_storage(container_name, 'container'): self.blob_service.put_block_blob_from_path( container_name, blob_name, path) return self.blob_service.make_blob_url(container_name, blob_name) else: return None except Exception as e: self.log.error(e) return None def delete_file_from_azure(self, container_name, blob_name): try: if self.create_container_in_storage(container_name, 'container'): self.blob_service.delete_blob(container_name, blob_name) except Exception as e: self.log.error(e) return None
class BlobServiceAdapter(Component): """The :class:`BlobServiceAdapter` class is a thin wrapper over azure.storage.BlobService. All the attributes of the wrapper stream are proxied by the adapter so it's possible to do ``adapter.create_container()`` instead of the long form ``adapter.blob_service.adapter()``. """ def __init__(self): self.blob_service = BlobService(account_name=self.util.get_config("storage.azure.account_name"), account_key=self.util.get_config("storage.azure.account_key"), host_base=self.util.get_config("storage.azure.blob_service_host_base")) def __getattr__(self, name): return getattr(self.blob_service, name) def create_container_in_storage(self, container_name, access="container"): """create a container if doesn't exist :type container_name: str|unicode :param container_name: Name of container to create. :type access: str|unicode :param access: Optional. Possible values include: container, blob :return: """ try: names = [x.name for x in self.blob_service.list_containers()] if container_name not in names: return self.blob_service.create_container(container_name, x_ms_blob_public_access=access) else: self.log.debug("container already exists in storage") return True except Exception as e: self.log.error(e) return False def upload_file_to_azure(self, container_name, blob_name, stream): """ Creates a new block blob from a file/stream, or updates the content of an existing block blob, with automatic chunking and progress notifications. :type container_name: str|unicode :param container_name: Name of existing container. :type blob_name: str | unicode :param blob_name: Name of blob to create or update. :type stream: file :param stream: Opened file/stream to upload as the blob content. """ try: if self.create_container_in_storage(container_name, 'container'): self.blob_service.put_block_blob_from_file(container_name, blob_name, stream) return self.blob_service.make_blob_url(container_name, blob_name) else: return None except Exception as e: self.log.error(e) return None def upload_file_to_azure_from_bytes(self, container_name, blob_name, blob): """ Creates a new block blob from an array of bytes, or updates the content of an existing block blob, with automatic chunking and progress notifications. :type container_name: str|unicode :param container_name: Name of existing container. :type blob_name: str|unicode :param blob_name: Name of blob to create or update. :type blob: bytes :param blob: Content of blob as an array of bytes. """ try: if self.create_container_in_storage(container_name, 'container'): self.blob_service.put_block_blob_from_bytes(container_name, blob_name, blob) return self.blob_service.make_blob_url(container_name, blob_name) else: return None except Exception as e: self.log.error(e) return None def upload_file_to_azure_from_text(self, container_name, blob_name, text): """ Creates a new block blob from str/unicode, or updates the content of an existing block blob, with automatic chunking and progress notifications. :type container_name: str|unicode :param container_name: Name of existing container. :type blob_name: str|unicode :param blob_name: Name of blob to create or update. :type text: str|unicode :param text: Text to upload to the blob. """ try: if self.create_container_in_storage(container_name, 'container'): self.blob_service.put_block_blob_from_text(container_name, blob_name, text) return self.blob_service.make_blob_url(container_name, blob_name) else: return None except Exception as e: self.log.error(e) return None def upload_file_to_azure_from_path(self, container_name, blob_name, path): """ Creates a new page blob from a file path, or updates the content of an existing page blob, with automatic chunking and progress notifications. :type container_name: str|unicode :param container_name: Name of existing container. :type blob_name: str|unicode :param blob_name: Name of blob to create or update. :type path: str|unicode :param path: Path of the file to upload as the blob content. """ try: if self.create_container_in_storage(container_name, 'container'): self.blob_service.put_block_blob_from_path(container_name, blob_name, path) return self.blob_service.make_blob_url(container_name, blob_name) else: return None except Exception as e: self.log.error(e) return None def delete_file_from_azure(self, container_name, blob_name): try: if self.create_container_in_storage(container_name, 'container'): self.blob_service.delete_blob(container_name, blob_name) except Exception as e: self.log.error(e) return None
blobs. blob_service: Nombre del servicio de gestion de blobs @class BlobService """ for i in blob_service.list_containers().containers: print("Nombre del contenedor: {}".format(i.name)) print("Url del contenedor: {}".format(i.url)) print("##############################") for j in blob_service.list_blobs(i.name).blobs: print("\tNombre del Blob: {}".format(j.name)) print("\tUrl del Blob: {}".format(j.url)) print("\t------------------------------") blob_service = BlobService(credentials.account_name, credentials.account_key) getContainersWithBlobs(blob_service) f_blob = open('Ejercicio10.txt', "w") for i in blob_service.list_containers().containers: f_blob.write("Nombre del contenedor: {}".format(i.name)) f_blob.write("Url del contenedor: {}".format(i.url)) f_blob.write("##############################") for j in blob_service.list_blobs(i.name).blobs: f_blob.write("\tNombre del Blob: {}".format(j.name)) f_blob.write("\tUrl del Blob: {}".format(j.url)) f_blob.write("\t------------------------------") f_blob.close() blob_service.put_blob('code', 'f_blob.txt', f_blob, 'BlockBlob')