示例#1
0
    def copy(self, source_path, dest_path, account=None, group_name=None):
        """Copy file from a path to another path. The azure url format is https://myaccount.blob.core.windows.net/mycontainer/myblob.
         Args:
             source_path(str): The path of the file to be copied.
             dest_path(str): The destination path where the file is going to be allocated.
         Raises:
             :exc:`~..DriverError`: if the file is not uploaded correctly.
        """
        if 'core.windows.net' not in source_path and 'core.windows.net' not in dest_path:
            self.logger.error(
                "Source or destination must be a azure storage url (format "
                "https://myaccount.blob.core.windows.net/mycontainer/myblob")
            raise DriverError

        # Check if source exists and can read
        if 'core.windows.net' in source_path:
            parse_url = _parse_url(source_path)
            key = self.storage_client.storage_accounts.list_keys(
                self.resource_group_name, parse_url.account).keys[0].value
            if parse_url.file_type == 'blob':
                bs = BlockBlobService(account_name=parse_url.account,
                                      account_key=key)
                return bs.get_blob_to_path(parse_url.container_or_share_name,
                                           parse_url.file, dest_path)
            elif parse_url.file_type == 'file':
                fs = FileService(account_name=parse_url.account,
                                 account_key=key)
                return fs.get_file_to_path(parse_url.container_or_share_name,
                                           parse_url.path, parse_url.file,
                                           dest_path)
            else:
                raise ValueError(
                    "This azure storage type is not valid. It should be blob or file."
                )
        else:
            parse_url = _parse_url(dest_path)
            key = self.storage_client.storage_accounts.list_keys(
                self.resource_group_name, parse_url.account).keys[0].value
            if parse_url.file_type == 'blob':
                bs = BlockBlobService(account_name=parse_url.account,
                                      account_key=key)
                return bs.create_blob_from_path(
                    parse_url.container_or_share_name, parse_url.file,
                    source_path)
            elif parse_url.file_type == 'file':
                fs = FileService(account_name=parse_url.account,
                                 account_key=key)
                return fs.create_file_from_path(
                    parse_url.container_or_share_name, parse_url.path,
                    parse_url.file, source_path)
            else:
                raise ValueError(
                    "This azure storage type is not valid. It should be blob or file."
                )
示例#2
0
 def delete(self, remote_file):
     """Delete file from the cloud. The azure url format is https://myaccount.blob.core.windows.net/mycontainer/myblob.
      Args:
          remote_file(str): The path of the file to be deleted.
      Raises:
          :exc:`~..DriverError`: if the file is not uploaded correctly.
     """
     if 'core.windows.net' not in remote_file:
         self.logger.error(
             "Source or destination must be a azure storage url (format "
             "https://myaccount.blob.core.windows.net/mycontainer/myblob")
         raise DriverError
     parse_url = _parse_url(remote_file)
     key = self.storage_client.storage_accounts.list_keys(
         self.resource_group_name, parse_url.account).keys[0].value
     if parse_url.file_type == 'blob':
         bs = BlockBlobService(account_name=parse_url.account,
                               account_key=key)
         return bs.delete_blob(parse_url.container_or_share_name,
                               parse_url.file)
     elif parse_url.file_type == 'file':
         fs = FileService(account_name=parse_url.account, account_key=key)
         return fs.delete_file(parse_url.container_or_share_name,
                               parse_url.path, parse_url.file)
     else:
         raise ValueError(
             "This azure storage type is not valid. It should be blob or file."
         )
示例#3
0
 def create_share_name(self, remote_folder):
     parse_url = _parse_url(remote_folder)
     key = self.storage_client.storage_accounts.list_keys(
         self.resource_group_name, parse_url.account).keys[0].value
     fs = FileService(account_name=parse_url.account, account_key=key)
     return fs.create_directory(
         share_name=parse_url.container_or_share_name,
         directory_name=parse_url.path)
示例#4
0
    def generate_url(self, remote_file):
        """Sign a remote file to distribute. The azure url format is https://myaccount.blob.core.windows.net/mycontainer/myblob.
         Args:
             remote_file(str): The blob that we want to sign.
        """
        parse_url = _parse_url(remote_file)
        key = self.storage_client.storage_accounts.list_keys(
            self.resource_group_name, parse_url.account).keys[0].value
        if parse_url.file_type == 'blob':
            bs = BlockBlobService(account_name=parse_url.account,
                                  account_key=key)

            sas_token = bs.generate_blob_shared_access_signature(
                parse_url.container_or_share_name,
                parse_url.file,
                permission=BlobPermissions.READ,
                expiry=datetime.utcnow() + timedelta(hours=24),
            )
            source_blob_url = bs.make_blob_url(
                container_name=parse_url.container_or_share_name,
                blob_name=parse_url.file,
                sas_token=sas_token)
            return source_blob_url
        elif parse_url.file_type == 'file':
            fs = FileService(account_name=parse_url.account, account_key=key)
            sas_token = fs.generate_file_shared_access_signature(
                share_name=parse_url.container_or_share_name,
                directory_name=parse_url.path,
                file_name=parse_url.file,
                permission=BlobPermissions.READ,
                expiry=datetime.utcnow() + timedelta(hours=24),
            )
            source_file_url = fs.make_file_url(
                share_name=parse_url.container_or_share_name,
                directory_name=parse_url.path,
                file_name=parse_url.file,
                sas_token=sas_token)
            return source_file_url
        else:
            raise ValueError(
                "This azure storage type is not valid. It should be blob or file."
            )
示例#5
0
 def create_container(self, remote_folder):
     parse_url = _parse_url(remote_folder)
     key = self.storage_client.storage_accounts.list_keys(
         self.resource_group_name, parse_url.account).keys[0].value
     bs = BlockBlobService(account_name=parse_url.account, account_key=key)
     return bs.create_container(container_name=remote_folder)