Logo Search packages:      
Sourcecode: s3ql version File versions  Download package

def s3ql::backends::common::AbstractBucket::prep_store_fh (   self,
  key,
  fh,
  metadata = None 
)
Prepare to store data in `fh` under `key`

`metadata` can be a dict of additional attributes to store with the
object. The method compresses and encrypts the data and returns a tuple
`(size, fn)`, where `fn` is a function that does the actual network
transaction and `size` is the size of the object after compression
and encryption.

Definition at line 276 of file common.py.

                                                   :
        """Prepare to store data in `fh` under `key`
        
        `metadata` can be a dict of additional attributes to store with the
        object. The method compresses and encrypts the data and returns a tuple
        `(size, fn)`, where `fn` is a function that does the actual network
        transaction and `size` is the size of the object after compression
        and encryption.
        """

        if not isinstance(key, str):
            raise TypeError('key must be of type str')

        # We always store metadata (even if it's just None), so that we can
        # verify that the object has been created by us when we call lookup().
        meta_buf = pickle.dumps(metadata, 2)

        meta_raw = dict()

        if self.passphrase:
            meta_raw['encryption'] = 'AES'
            nonce = struct.pack(b'<f', time.time() - time.timezone) + bytes(key)
            meta_raw['meta'] = b64encode(encrypt(meta_buf, self.passphrase, nonce))
        else:
            meta_raw['encryption'] = 'None'
            meta_raw['meta'] = b64encode(meta_buf)

        if self.compression == 'zlib':
            compr = zlib.compressobj(9)
            meta_raw['compression'] = 'ZLIB'
        elif self.compression == 'bzip2':
            compr = bz2.BZ2Compressor(9)
            meta_raw['compression'] = 'BZIP2'
        elif self.compression == 'lzma':
            compr = lzma.LZMACompressor(options={ 'level': 7 })
            meta_raw['compression'] = 'LZMA'
        elif not self.compression:
            compr = DummyCompressor()
            meta_raw['compression'] = 'None'
        else:
            raise ValueError('Invalid compression algorithm')

        # We need to generate a temporary copy to determine the size of the
        # object (which needs to transmitted as Content-Length)
        tmp = tempfile.TemporaryFile()
        fh.seek(0)
        if self.passphrase:
            compress_encrypt_fh(fh, tmp, self.passphrase, nonce, compr)
        else:
            compress_fh(fh, tmp, compr)
        tmp.seek(0, os.SEEK_END)
        size = tmp.tell()
        tmp.seek(0)
        return (size, lambda: self.raw_store(key, tmp, meta_raw))


Generated by  Doxygen 1.6.0   Back to index