Beispiel #1
0
def get_blob_properties(filename):
    logger = logging.getLogger(__name__)
    props = BlobProperties()
    content = ContentSettings()
    logger.debug('Settings props for file %s', filename)
    props.name = filename
    if '.json' in filename:
        content.content_type = 'application/json'
    elif '.html' in filename:
        content.content_type = 'text/html'
    else:
        content.content_type = 'text/plain'
    logger.debug('Content-type set to %s', content.content_type)
    content.content_disposition = f'inline; filename={filename}'
    props.content_settings = content
    return props
Beispiel #2
0
        def update(self, area, selector, content_type, buffer):
            assert area is not None, 'area is none; should already be validated'

            area_config = config.load_area(area)
            
            storage_config = config.load_storage(area_config['storage'])
            
            area = area.lower()

            # httplib.HTTPConnection.debuglevel = 1
            # http.client.HTTPConnection.debuglevel = 1

            blob_service = BlockBlobService(account_name=storage_config['name'], account_key=storage_config['key1'])

            hash = base64.b64encode(hashlib.md5(buffer).digest())

            content_settings = ContentSettings(content_md5=hash)
            if content_type is not None and len(content_type) > 0:
                content_settings.content_type = content_type

            blob_service.create_blob_from_bytes(
                area_config['container'],
                selector,
                buffer,
                content_settings=content_settings,
                validate_content=False
            )

            return hash
Beispiel #3
0
    def sync_folder(self, folder, del_remote=False, del_local=False):
        root_path = os.path.join(self.local_static_root, folder)
        print('Local folder path: ' + root_path)

        prefix = ''
        local_files = set()
        for root, dirs, files in os.walk(root_path, followlinks=True):
            print('-' * 70)
            head = root
            prefix = ''
            while head != self.local_static_root:
                head, tail = os.path.split(head)
                prefix = '/'.join([tail, prefix])

            print(prefix)
            if prefix.startswith('_'):
                print('ignored')
                continue
            files = set(prefix + name for name in files)
            print('files:', files)
            local_files.update(files)

        print('=' * 70)
        remote_blobs = self.block_blob_service.list_blobs(self.blob_container,
                                                          prefix=folder)
        remote_files = set(blob.name for blob in remote_blobs)
        print('local files:', local_files)
        print('remote blobs: ', remote_files)

        print('=' * 70)
        local_extra = local_files - remote_files
        remote_extra = remote_files - local_files
        # print('new files:', new_files)
        # print('del files:', del_files)

        for fname in local_extra:
            local_path = os.path.join(self.local_static_root, fname)
            remote_path = fname

            try:
                if del_local:
                    print(' ^^ \tDEL\t', local_path, ' <-- ', '(local)')
                    os.remove(local_path)
                else:
                    print(' ^^ \tUPLOAD\t', local_path, ' --> ', remote_path)
                    content = None
                    content_type = mimetypes.guess_type(local_path)[0]
                    if content_type is not None:
                        content = ContentSettings()
                        content.content_type = content_type
                    self.block_blob_service.create_blob_from_path(
                        container_name=self.blob_container,
                        blob_name=remote_path,
                        file_path=local_path,
                        content_settings=content)
            except Exception as err:
                print(err)

        for fname in remote_extra:
            local_path = os.path.join(self.local_static_root, fname)
            remote_path = fname
            try:
                if del_remote:
                    print(' xx \tDEL\t', '(remote)', ' --> ', remote_path)
                    self.block_blob_service.delete_blob(
                        container_name=self.blob_container,
                        blob_name=remote_path)
                else:
                    print(' ^^ \tDOWNLOAD\t', local_path, ' <-- ', remote_path)
                    self.block_blob_service.get_blob_to_path(
                        container_name=self.blob_container,
                        blob_name=remote_path,
                        file_path=local_path)
            except Exception as err:
                print(err)