Backblaze-B2
view release on metacpan or search on metacpan
lib/Backblaze/B2.pm view on Meta::CPAN
#sub api { $_[0]->{api} }
sub downloadUrl { join "/", $_[0]->api->downloadUrl, $_[0]->name }
sub id { $_[0]->{bucketId} }
sub type { $_[0]->{bucketType} }
sub account { $_[0]->{parent} }
sub _new_file {
my( $self, %options ) = @_;
# Should this one magically unwrap AnyEvent::condvar objects?!
#warn $self->{file_class};
#use Data::Dumper;
#warn Dumper \%options;
$self->{file_class}->new(
%options,
api => $self->api,
bucket => $self
);
}
=head2 C<< ->files( %options ) >>
Lists the files contained in this bucket
my @files = $bucket->files(
startFileName => undef,
);
By default it returns only the first 1000
files, but see the C<allFiles> parameter.
=over 4
=item C<< allFiles >>
allFiles => 1
Passing in a true value for this parameter will make
as many API calls as necessary to fetch all files.
=back
=cut
sub files {
my( $self, %options ) = @_;
$options{ maxFileCount } ||= 1000;
#$options{ startFileName } ||= undef;
$self->api->asyncApi->list_all_file_names(
bucketId => $self->id,
%options,
)->then( sub {
my( $ok, $msg, @res ) = @_;
$ok, $msg, map { $self->_new_file( %$_, bucket => $self ) } @res
})
}
=head2 C<< ->upload_file( %options ) >>
Uploads a file into this bucket, potentially creating
a new file version.
my $new_file = $bucket->upload_file(
file => 'some/local/file.txt',
target_file => 'the/public/name.txt',
);
=over 4
=item C<< file >>
Local name of the source file. This file will be loaded
into memory in one go.
=item C<< target_file >>
Name of the file on the B2 API. Defaults to the local name.
The target file name will have backslashes replaced by forward slashes
to comply with the B2 API.
=item C<< mime_type >>
Content-type of the stored file. Defaults to autodetection by the B2 API.
=item C<< content >>
If you don't have the local content in a file on disk, you can
pass the content in as a string.
=item C<< mtime >>
Time in miliseconds since the epoch to when the content was created.
Defaults to the current time.
=item C<< sha1 >>
Hexdigest of the SHA1 of the content. If this is missing, the SHA1
will be calculated upon upload.
=back
=cut
sub upload_file {
my( $self, %options ) = @_;
my $api = $self->api->asyncApi;
$api->get_upload_url(
bucketId => $self->id,
)->then(sub {
my( $ok, $msg, $upload_handle ) = @_;
$api->upload_file(
%options,
handle => $upload_handle
);
})->then( sub {
my( $ok, $msg, @res ) = @_;
(my $res) = map { $self->_new_file( %$_, bucket => $self ) } @res;
$ok, $msg, $res
});
}
=head2 C<< ->download_file_by_name( %options ) >>
Downloads a file from this bucket by name:
my $content = $bucket->download_file_by_name(
fileName => 'the/public/name.txt',
);
This saves you searching through the list of existing files
if you already know the filename.
=cut
sub download_file_by_name {
my( $self, %options ) = @_;
$self->api->asyncApi->download_file_by_name(
bucketName => $self->name,
%options
);
}
=head2 C<< ->get_download_authorization( %options ) >>
Downloads a file from this bucket by name:
my $authToken = $bucket->get_download_authorization(
fileNamePrefix => '/members/downloads/',
validDurationInSeconds => 300, # five minutes
);
This returns an authorization token that can download files with the
given prefix.
=cut
sub get_download_authorization {
my( $self, %options ) = @_;
$self->api->asyncApi->get_download_authorization(
bucketId => $self->id,
%options
);
}
=head2 C<< ->api >>
Returns the underlying API object
=cut
sub api { $_[0]->{api} }
lib/Backblaze/B2.pm view on Meta::CPAN
};
};
# Invoke the newly installed method
goto &$new_method;
};
sub new {
my( $class, %options ) = @_;
my $self = {
impl => Backblaze::B2::v1::Bucket->new(%options),
file_class => $options{ file_class },
};
bless $self => $class,
}
sub impl { $_[0]->{impl} }
sub _new_file {
my( $self, %options ) = @_;
$self->{file_class}->new(
%options,
api => $self->api,
bucket => $self
);
}
=head2 C<< ->files( %options ) >>
Lists the files contained in this bucket
my @files = $bucket->files(
startFileName => undef,
);
By default it returns only the first 1000
files, but see the C<allFiles> parameter.
=over 4
=item C<< allFiles >>
allFiles => 1
Passing in a true value for this parameter will make
as many API calls as necessary to fetch all files.
=back
=cut
sub files {
my( $self, %options ) = @_;
map { $self->_new_file( impl => $_ ) }
Backblaze::B2::v1::payload $self->impl->files( %options );
}
=head2 C<< ->upload_file( %options ) >>
Uploads a file into this bucket, potentially creating
a new file version.
my $new_file = $bucket->upload_file(
file => 'some/local/file.txt',
target_file => 'the/public/name.txt',
);
=over 4
=item C<< file >>
Local name of the source file. This file will be loaded
into memory in one go.
=item C<< target_file >>
Name of the file on the B2 API. Defaults to the local name.
The target file name will have backslashes replaced by forward slashes
to comply with the B2 API.
=item C<< mime_type >>
Content-type of the stored file. Defaults to autodetection by the B2 API.
=item C<< content >>
If you don't have the local content in a file on disk, you can
pass the content in as a string.
=item C<< mtime >>
Time in miliseconds since the epoch to when the content was created.
Defaults to the current time.
=item C<< sha1 >>
Hexdigest of the SHA1 of the content. If this is missing, the SHA1
will be calculated upon upload.
=back
=cut
sub upload_file {
my( $self, %options ) = @_;
Backblaze::B2::v1::payload $self->impl->upload_file( %options );
}
=head2 C<< ->download_file_by_name( %options ) >>
Downloads a file from this bucket by name:
my $content = $bucket->download_file_by_name(
file => 'the/public/name.txt',
);
This saves you searching through the list of existing files
if you already know the filename.
=cut
sub download_file_by_name {
my( $self, %options ) = @_;
return Backblaze::B2::v1::payload $self->impl->download_file_by_name(
%options
)
}
=head2 C<< ->api >>
Returns the underlying API object
=cut
sub api { $_[0]->{api} }
package Backblaze::B2::v1::File;
use strict;
#use Scalar::Util 'weaken'; # do we really want to weaken our link?!
# The bucket doesn't hold a ref to us, so we don't want to weaken it
sub new {
my( $class, %options ) = @_;
#weaken $options{ bucket };
#warn "$class: " . join ",", sort keys %options;
bless \%options => $class,
}
sub name { $_[0]->{fileName} }
sub id { $_[0]->{fileId} }
sub action { $_[0]->{action} }
sub bucket { $_[0]->{bucket} }
sub size { $_[0]->{size} }
sub downloadUrl { join "/", $_[0]->bucket->downloadUrl, $_[0]->name }
package Backblaze::B2::v1::File::Synchronized;
use strict;
use Carp qw(croak);
#use Scalar::Util 'weaken'; # do we really want to weaken our link?!
# The bucket doesn't hold a ref to us, so we don't want to weaken it
sub new {
my( $class, %options ) = @_;
#weaken $options{ bucket };
#warn "$class: " . join ",", sort keys %options;
( run in 1.089 second using v1.01-cache-2.11-cpan-b16cb0d3907 )