1
#!/usr/bin/env python2.5
3
Blob File Server client
5
import hashlib, os, email
7
from blobs.client.httpclient import HTTPClient, FileProxy
9
class BlobClient(object):
10
def __init__(self, url):
12
Creates a new client object
13
You must pass in the url to the Blob server
15
self.client = HTTPClient(url)
17
def getFile(self, hash, output):
18
url = "/files/%s" % hash
20
res = self.client.get(url)
23
raise ValueError("File not found")
25
if hasattr(output, "write"):
28
if os.path.isdir(output):
29
contentDisp = res.getheader("content-disposition", None)
30
msg = email.message_from_string(str(res.msg))
31
filename = msg.get_filename()
32
fp = open(filename, "w")
34
fp = open(output, "w")
42
def putFile(self, filename, formparam="file", size = None,
45
if inputFileObj is None:
46
inputFileObj = open(filename)
49
size = os.path.getsize(filename)
51
res = self.client.post("/files",
52
{formparam: FileProxy(filename = filename,
53
file_obj = inputFileObj, size = size),
54
'filename': filename})
56
return res.status, res.reason
59
res = self.client.get("/files.json")
61
return simplejson.load(res)
63
if __name__ == "__main__":
64
from optparse import OptionParser
67
parser = OptionParser()
68
parser.set_defaults(action=None,
69
server=os.environ.get("BLOB_SERVER"),
72
parser.add_option("-s", "--server", dest="server",
73
help="Specify the server to connect to. Alternatively set the BLOB_SERVER environment variable")
74
parser.add_option("-l", "--list", dest="action", action="store_const",
75
const="list", help="Print file listing")
76
parser.add_option("-u", "--upload", dest="action", action="store_const",
78
help="Upload the specified file(s) to the server")
79
parser.add_option("-d", "--download", dest="action", action="store_const",
81
help="Download the specified list of files given as a list of hashes")
82
parser.add_option("-o", "--output", dest="output",
83
help="""Where to save the downloaded file(s). If multiple files
84
are to be downloaded, then this must be a directory.
86
Otherwise, if a single file is to be downloaded, this can be the name of a file
87
to write to. It can also be the directory to save the file into, in which case the original filename will be preserved. It can also be '-' then output will be written to stdout""")
89
options, args = parser.parse_args()
91
if not options.server:
92
parser.error("Need to specify a server")
94
c = BlobClient(options.server)
96
if options.action == "list":
97
for h, size, filename in c.listFiles():
100
elif options.action == "upload":
104
elif options.action == "download":
106
if options.output == "-":
107
c.getFile(args[0], sys.stdout)
108
elif os.path.isdir(options.output):
109
c.getFile(args[0], options.output)
111
c.getFile(args[0], open(options.output, "w"))
113
if options.output == "-":
114
parser.error("You cannot output to stdout when multiple files are downloaded")
115
elif not os.path.isdir(options.output):
116
parser.error("You must output to a directory when multiple files are downloaded")
119
c.getFile(h, options.output)