Checking in changes prior to tagging of version 2.30.
[MogileFS-Utils.git] / mogupload
blob086455b81efb45468c326ff418b34f1030036e24
1 #!/usr/bin/perl
3 =head1 NAME
5 mogupload -- Upload data to a MogileFS installation
7 =head1 SYNOPSIS
9 $ mogupload [options]
10 $ mogupload [options] --file="-" < filename
12 $ mogupload --trackers=host --domain=foo --class=bar \
13 --key="/hello.jpg" --file="input.jpg"
15 $ echo "why hello" | mogupload [opts] --key="world" --file="-"
17 =head1 OPTIONS
19 =over
21 =item --trackers=host1:7001,host2:7001
23 Use these MogileFS trackers to negotiate with.
25 =item --domain=<domain>
27 Set the MogileFS domain to use.
29 =item --class=<class>
31 Set the class to use. Will use default class if not specified
33 =item --key="<key>"
35 A key to store the file under. Can be an arbitrary string.
37 =item --file="<filename|->"
39 A local file to upload. If '-', read file from STDIN instead.
41 =back
43 =head1 AUTHOR
45 Dormando E<lt>L<dormando@rydia.net>E<gt>
47 =head1 LICENSE
49 Licensed for use and redistribution under the same terms as Perl itself.
51 =cut
53 use strict;
54 use warnings;
56 use lib './lib';
57 use MogileFS::Utils;
59 my $util = MogileFS::Utils->new;
60 my $usage = "--trackers=host --domain=foo --key='/hello.jpg' --file='./hello.jpg'";
61 my $c = $util->getopts($usage, qw/class=s key=s file=s/);
63 my $mogc = $util->client;
65 my $filename = $c->{file};
66 die "Must specify a file to upload with --file" unless $filename;
68 my $fh;
69 my $size;
70 if ($filename eq '-') {
71 $fh = *STDIN;
72 } else {
73 $size = -s $filename;
74 die "Could not stat " . $filename unless defined $size;
75 open($fh, "< $filename") or die "Could not open " . $filename;
78 my $mf = $mogc->new_file($c->{key}, $c->{class}, $size);
79 if ($mogc->errcode) {
80 die "Error opening MogileFS file: " . $mogc->errstr;
83 my $buf;
84 while (my $read = read($fh, $buf, 1024 * 1024)) {
85 die "error reading file" unless defined $read;
86 $mf->print($buf);
89 unless ($mf->close) {
90 die "Error writing file: " . $mogc->errcode . ": " . $mogc->errstr;