class AzureBlobService(): def __init__(self, logger, account_name, account_key): self.logger = logger self.blob_service = BlobService(account_name=account_name, account_key=account_key) self.pbar = None def azure_sync(self, root_path, container, check=False): return_code = 0 if not os.path.exists(root_path): self.logger.error("Error in sync file %s to azure. Details: No such file or directory" % (root_path)) return_code = 1 else: dt_start = datetime.now() self.logger.info("Syncing files from %s to azure container %s" % (root_path, container)) for root, dirs, files in os.walk(root_path, topdown=False): for name in files: blob_metadata = {} path = os.path.join(root, name) mtime = str(os.path.getmtime(path)) try: self.logger.debug("file: %s - Checking file modification time with azure API" % (path)) blob_metadata = self.blob_service.get_blob_metadata(container, path) except Exception, e: if type(e).__name__ != 'WindowsAzureMissingResourceError': self.logger.error("file: %s - Can't check file modification time with azure API. Details: %s - %s" % (path, type(e).__name__, str(e))) return_code = return_code + 1 continue else: pass if ('x-ms-meta-mtime' not in blob_metadata) or (blob_metadata['x-ms-meta-mtime'] != mtime): return_code = return_code + self.azure_send(container, path, mtime) dt_finish = datetime.now() dt_elapsed = dt_finish - dt_start if return_code == 0: self.logger.info("Sync files from %s to azure finished successfully. Time to sync: %s" % (root_path, dt_elapsed)) else: self.logger.error("Sync files from %s to azure finished with %i errors. Time to sync: %s" % (root_path, return_code, dt_elapsed)) return return_code
class AzureBlobStorage(Storage): def __init__(self, account='nyxstorage', container='pxo'): self.base_storage_uri = 'http://%s.blob.core.windows.net/%s/' % ( account, container) self.blob_service = BlobService( account, get_env_variable('AZURE_BLOB_STORAGE_KEY')) self.container = container def _open(self, name, mode='rb'): data = self.blob_service.get_blob(self.container, name) return ContentFile(data) def _save(self, name, content): _file = content.read() file_name = content.name[-35:] self.blob_service.put_blob( self.container, file_name, _file, x_ms_blob_type='BlockBlob') return self.base_storage_uri + file_name def create_container(self, container_name): result = self.blob_service.create_container( container_name, x_ms_blob_public_access='container') return result def delete(self, name): self.blob_service.delete_blob(self.container, name) def exists(self, name): try: self.blob_service.get_blob_properties(self.container, name) except: return False else: return True def get_available_name(self, name): return name def get_blobs(self): blobs = self.blob_service.list_blobs(self.container) return blobs def get_valid_name(self, name): return name def modified_time(self, name): metadata = self.blob_service.get_blob_metadata(self.container, name) modified_time = float(metadata.get('x-ms-meta-modified_time')) return datetime.fromtimestamp(modified_time) def set_public_container(self, container_name): result = self.blob_service.set_container_acl( container_name, x_ms_blob_public_access='container') return result def size(self, name): properties = self.blob_service.get_blob_properties( self.container, name) return properties.get('content-length') def url(self, name): blob = self.blob_service.list_blobs(self.container, prefix=name) return blob.blobs[0].url