with open(file) as fp:
for hash in missing:
- offset = hashes.index(unhexlify(hash)) * BLOCK_SIZE
+ offset = hashes.index(unhexlify(hash)) * blocksize
fp.seek(offset)
- block = fp.read(BLOCK_SIZE)
+ block = fp.read(blocksize)
client.create_object(container, '.upload', StringIO(block))
client.create_object_by_hashmap(container, object, map)
+
+def download(client, container, object, file):
+
+ meta = client.retrieve_container_metadata(container)
+ blocksize = int(meta['x-container-block-size'])
+ blockhash = meta['x-container-block-hash']
+
+ if os.path.isfile(file):
+ size = os.path.getsize(file)
+ hashes = HashMap(blocksize, blockhash)
+ hashes.load(file)
+ else:
+ size = 0
+ hashes = []
+
+ map = client.retrieve_object_hashmap(container, object)
+
+ with open(file, 'a') as fp:
+ for i, h in enumerate(map):
+ if i < len(hashes) and h == hashes[i]:
+ continue
+ start = i * blocksize
+ end = '' if i == len(map) - 1 else (i + 1) * blocksize
+ data = client.retrieve_object(container, object, range='bytes=%s-%s' % (start, end))
+ fp.seek(start)
+ fp.write(data)
from datetime import datetime
from lib.client import Pithos_Client, Fault
from lib.util import get_user, get_auth, get_server, get_api
-from lib.transfer import upload
+from lib.transfer import upload, download
import json
import logging
def execute(self, path=''):
container, sep, object = path.partition('/')
- args = {'restricted':self.restricted}
+ args = {'restricted': self.restricted}
if getattr(self, 'until'):
t = _time.strptime(self.until, self.format)
args['until'] = int(_time.mktime(t))
args = self._build_args(attrs)
args['format'] = 'json' if self.detail else 'text'
if self.range:
- args['range'] = 'bytes=%s' %self.range
+ args['range'] = 'bytes=%s' % self.range
if getattr(self, 'if_range'):
args['if-range'] = 'If-Range:%s' % getattr(self, 'if_range')
class Send(Command):
syntax = '<file> <container>[/<prefix>]'
description = 'upload file to container (using prefix)'
-
- def execute(self, file, prefix):
- container, sep, prefix = prefix.partition('/')
+
+ def execute(self, file, path):
+ container, sep, prefix = path.partition('/')
upload(self.client, file, container, prefix)
+@cli_command('receive')
+class Receive(Command):
+ syntax = '<container>/<object> <file>'
+ description = 'download object to file'
+
+ def execute(self, path, file):
+ container, sep, object = path.partition('/')
+ download(self.client, container, object, file)
+
def print_usage():
cmd = Command('', [])
parser = cmd.parser