Backblaze-B2
view release on metacpan or search on metacpan
lib/Backblaze/B2.pm view on Meta::CPAN
=cut
sub new {
my( $class, %options ) = @_;
$options{ version } ||= 'v1';
$class = "$class\::$options{ version }";
$class->new( %options );
};
=head1 SETUP
=over 4
=item 0. Have a telephone / mobile phone number you're willing to
share with Backblaze
=item 1. Register at for Backblaze B2 Cloud Storage at
L<https://secure.backblaze.com/account_settings.htm?showPhone=true>
=item 2. Add the phone number to your account at
L<https://secure.backblaze.com/account_settings.htm?showPhone=true>
=item 3. Enable Two-Factor verification through your phone at
L<https://secure.backblaze.com/account_settings.htm?showPhone=true>
=item 4. Create a JSON file named C<B2.credentials>
This file should live in your
home directory
with the application key and the account key:
{ "accountId": "...",
"applicationKey": ".............."
}
=back
=cut
package Backblaze::B2::v1;
use strict;
use Carp qw(croak);
=head1 NAME
Backblaze::B2::v1 - Backblaze B2 API account
=head1 METHODS
=head2 C<< ->new %options >>
my $b2 = Backblaze::B2::v1->new(
api => 'Backblaze::B2::v1::Synchronous', # the default
);
Creates a new instance. Depending on whether you pass in
C<<Backblaze::B2::v1::Synchronous>> or C<<Backblaze::B2::v1::AnyEvent>>,
you will get a synchronous or asynchronous API.
The synchronous API is what is documented here, as this is the
most likely use case.
my @buckets = $b2->buckets();
for( @buckets ) {
...
}
The asynchronous API is identical to the synchronous API in spirit, but
will return L<Promises> . These condvars usually return
two or more parameters upon completion:
my $results = $b2->buckets();
$results->then( sub{
my( @buckets ) = @_;
for( @buckets ) {
...
}
}
The asynchronous API puts the burden of error handling into your code.
=cut
use vars '$API_BASE';
$API_BASE = 'https://api.backblazeb2.com/b2api/v1/';
sub new {
my( $class, %options ) = @_;
# Hrr. We need to get at an asynchronous API here and potentially
# wrap the results to synchronous results in case the user wants them.
# Turtles all the way down, this means we can't reuse calls into ourselves...
$options{ api } ||= 'Backblaze::B2::v1::Synchronous';
if( ! ref $options{ api }) {
eval "require $options{ api }";
my $class = delete $options{ api };
$options{ api } = $class->new(%options);
};
if( $options{ api }->isAsync ) {
$options{ bucket_class } ||= 'Backblaze::B2::v1::Bucket';
$options{ file_class } ||= 'Backblaze::B2::v1::File';
} else {
$options{ bucket_class } ||= 'Backblaze::B2::v1::Bucket::Synchronized';
$options{ file_class } ||= 'Backblaze::B2::v1::File::Synchronized';
};
bless \%options => $class
}
sub read_credentials {
my( $self, @args ) = @_;
$self->api->read_credentials(@args)
}
sub authorize_account {
my( $self, @args ) = @_;
$self->api->authorize_account(@args)
}
sub _new_bucket {
my( $self, %options ) = @_;
$self->{bucket_class}->new(
%options,
api => $self->api,
parent => $self,
file_class => $self->{file_class}
)
}
sub await($) {
my $promise = $_[0];
my @res;
if( $promise->is_unfulfilled ) {
require AnyEvent;
my $await = AnyEvent->condvar;
$promise->then(sub{
$await->send(@_);
}, sub {
warn "@_";
});
@res = $await->recv;
} else {
warn "Have results already";
@res = @{ $promise->result }
}
@res
};
lib/Backblaze/B2.pm view on Meta::CPAN
);
Returns a L<Backblaze::B2::Bucket> object that has the given ID. It
does not make an HTTP request to fetch the name and status of that bucket.
=cut
sub bucket_from_id {
my( $self, $bucket_id ) = @_;
$self->_new_bucket( bucketId => $bucket_id );
}
=head2 C<< ->create_bucket >>
my $new_bucket = $b2->create_bucket(
name => 'my-new-bucket', # only /[A-Za-z0-9-]/i are allowed as bucket names
type => 'allPrivate', # or allPublic
);
print sprintf "Created new bucket %s\n", $new_bucket->id;
Creates a new bucket and returns it.
=cut
sub create_bucket {
my( $self, %options ) = @_;
$options{ type } ||= 'allPrivate';
my $b = $self->api->asyncApi->create_bucket(
bucketName => $options{ name },
bucketType => $options{ type },
)->then( sub {
my( $bucket ) = @_;
$self->_new_bucket( %$bucket );
});
if( !$self->api->isAsync ) {
Backblaze::B2::v1::payload $b
}
}
=head2 C<< ->api >>
Returns the underlying API object
=cut
sub api { $_[0]->{api} }
1;
package Backblaze::B2::v1::Bucket;
use strict;
use Scalar::Util 'weaken';
sub new {
my( $class, %options ) = @_;
weaken $options{ parent };
# Whoa! We assume that the async version has the same class name
# as the synchronous version and just strip it off.
$options{ file_class } =~ s!::Synchronized$!!;
bless \%options => $class,
}
sub name { $_[0]->{bucketName} }
#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 {
( run in 2.135 seconds using v1.01-cache-2.11-cpan-9581c071862 )