Backblaze-B2

 view release on metacpan or  search on metacpan

.gitignore  view on Meta::CPAN

*.tar.gz
*.bak
pm_to_blib
blib/
BackBlaze-B2-*
BackBlaze-B2-*/
.releaserc
cover_db
firefox-versions/
MYMETA.*
credentials.json
app-credentials.json

MANIFEST.SKIP  view on Meta::CPAN

^t.*sessions
^cover_db
^.*\.log
^.*\.swp$
^jar/
^cpan/
^MYMETA
^.releaserc
^Backblaze-B2-.*/
^Backblaze-B2-.*.tar.gz$
^app-credentials.json
^credentials.json

README  view on Meta::CPAN

      https://secure.backblaze.com/account_settings.htm?showPhone=true

    2. Add the phone number to your account at

      https://secure.backblaze.com/account_settings.htm?showPhone=true

    3. Enable Two-Factor verification through your phone at

      https://secure.backblaze.com/account_settings.htm?showPhone=true

    4. Create a JSON file named B2.credentials

      This file should live in your home directory with the application key
      and the account key:

          { "accountId":      "...",
            "applicationKey": ".............."
          }

NAME

README.mkdn  view on Meta::CPAN

    [https://secure.backblaze.com/account\_settings.htm?showPhone=true](https://secure.backblaze.com/account_settings.htm?showPhone=true)

- 2. Add the phone number to your account at

    [https://secure.backblaze.com/account\_settings.htm?showPhone=true](https://secure.backblaze.com/account_settings.htm?showPhone=true)

- 3. Enable Two-Factor verification through your phone at

    [https://secure.backblaze.com/account\_settings.htm?showPhone=true](https://secure.backblaze.com/account_settings.htm?showPhone=true)

- 4. Create a JSON file named `B2.credentials`

    This file should live in your
    home directory
    with the application key and the account key:

        { "accountId":      "...",
          "applicationKey": ".............."
        }

# NAME

examples/authenticate.pl  view on Meta::CPAN

#!perl -w
use strict;
use JSON::XS;
use Backblaze::B2;
use Getopt::Long;

GetOptions(
    'c|credentials:s' => \my $credentials_file,
    'o|application-credentials:s' => \my $app_credentials_file,
);

$app_credentials_file ||= './app-credentials.json';

=head1 SYNOPSIS

=head1 SEE ALSO

L<https://www.backblaze.com/b2/docs/b2_authorize_account.html>

=cut

my $b2 = Backblaze::B2->new(
    version => 'v1',
    log_message => sub { warn sprintf "[%d] %s\n", @_; },
);

my $credentials = $b2->read_credentials( $credentials_file );

use Data::Dumper;
my ($app_credentials) = $b2->authorize_account(
    %$credentials
);

open my $fh, '>', $app_credentials_file
    or die "Couldn't write credentials to '$app_credentials_file'";
binmode $fh;
print {$fh} encode_json( $app_credentials );

examples/create-bucket.pl  view on Meta::CPAN

#!perl -w
use strict;
use JSON::XS;
use Backblaze::B2;
use Getopt::Long;

GetOptions(
    'c|credentials:s' => \my $credentials_file,
    'o|application-credentials:s' => \my $app_credentials,
);

$app_credentials ||= './app-credentials.json';

my ($bucket_name) = @ARGV;

=head1 SYNOPSIS

=head1 SEE ALSO

L<https://www.backblaze.com/b2/docs/b2_authorize_account.html>

=cut

my $b2 = Backblaze::B2->new(
    version => 'v1',
);

my $credentials = $b2->read_credentials( $credentials_file );
if( ! $credentials->{authorizationToken}) {
    $b2->authorize_account(%$credentials);
};

use Data::Dumper;
warn Dumper $b2->create_bucket(bucketName => $bucket_name );

examples/delete-bucket.pl  view on Meta::CPAN

#!perl -w
use strict;
use JSON::XS;
use Backblaze::B2;
use Getopt::Long;

GetOptions(
    'c|credentials:s' => \my $credentials_file,
);

=head1 SYNOPSIS

=cut

my $b2 = Backblaze::B2->new(
    version => 'v1',
);

my $credentials = $b2->read_credentials( $credentials_file );
if( ! $credentials->{authorizationToken}) {
    $b2->authorize_account(%$credentials);
};

use Data::Dumper;

for my $id (@ARGV) {
    print "Deleting bucket $id\n";
    print Dumper $b2->delete_bucket(bucketId => $id );
};

examples/download-file.pl  view on Meta::CPAN

#!perl -w
use strict;
use JSON::XS;
use Backblaze::B2;
use Getopt::Long;

GetOptions(
    'c|credentials:s' => \my $credentials_file,
    'o|target-base:s' => \my $target_base,
);
$target_base ||= '.';

my ($bucket_name, @files) = @ARGV;

=head1 SYNOPSIS

=cut

my $b2 = Backblaze::B2->new(
    version => 'v1',
    log_message => sub { warn sprintf "[%d] %s\n", @_; },
);

my $credentials = $b2->read_credentials( $credentials_file );
if( ! $credentials->{authorizationToken}) {
    $b2->authorize_account(%$credentials);
};

(my $bucket) = grep { $_->name =~ /$bucket_name/ or $_->id eq $bucket_name }
               sort { $a->name cmp $b->name }
               $b2->buckets;

if( ! $bucket ) {
    die "No bucket found with name matching '$bucket_name'";
};

examples/list-bucket-contents.pl  view on Meta::CPAN

#!perl -w
use strict;
use JSON::XS;
use Backblaze::B2;
use Getopt::Long;

GetOptions(
    'c|credentials:s' => \my $credentials_file,
);

my ($bucket_name) = @ARGV;

=head1 SYNOPSIS

=cut

my $b2 = Backblaze::B2->new(
    version => 'v1',
    log_message => sub { warn sprintf "[%d] %s\n", @_; },
);

my $credentials = $b2->read_credentials( $credentials_file );
if( ! $credentials->{authorizationToken}) {
    $b2->authorize_account(%$credentials);
};

for my $bucket ($b2->buckets()) {
    print join "\t", $bucket->name, $bucket->type, $bucket->id;
    print "\n";
    
    for my $file ($bucket->files) {
        print $file->name, "\n";
    };
};

examples/list-buckets.pl  view on Meta::CPAN

#!perl -w
use strict;
use JSON::XS;
use Backblaze::B2;
use Getopt::Long;

GetOptions(
    'c|credentials:s' => \my $credentials_file,
);

my ($bucket_name) = @ARGV;

=head1 SYNOPSIS

=cut

my $b2 = Backblaze::B2->new(
    version => 'v1',
    log_message => sub { warn sprintf "[%d] %s\n", @_; },
);

my $credentials = $b2->read_credentials( $credentials_file );
if( ! $credentials->{authorizationToken}) {
    $b2->authorize_account(%$credentials);
};

for my $bucket ($b2->buckets()) {
    print join "\t", $bucket->name, $bucket->type, $bucket->id;
    print "\n";
};

examples/upload-file-async.pl  view on Meta::CPAN

#!perl -w
use strict;
use JSON::XS;
use Backblaze::B2;
use Getopt::Long;
use Promises 'collect';

GetOptions(
    'c|credentials:s' => \my $credentials_file,
);

my ($bucket_id, @files) = @ARGV;

=head1 SYNOPSIS

=cut

my $b2 = Backblaze::B2->new(
    version => 'v1',

examples/upload-file-async.pl  view on Meta::CPAN

        require AnyEvent;
        my $await = AnyEvent->condvar;
        $promise->then(sub{ $await->send(@_)});
        @res = $await->recv;
    } else {
        @res = @{ $promise->result }
    }
    @res
};

my $credentials = $b2->read_credentials( $credentials_file );
if( ! $credentials->{authorizationToken}) {
    await $b2->authorize_account(%$credentials);
};

my $bucket = $b2->bucket_from_id( $bucket_id );
    
await collect( 
    map {
        my $file = $_;
        $bucket->upload_file(
            bucketId => $bucket_id,
            file => $file,

examples/upload-file.pl  view on Meta::CPAN

#!perl -w
use strict;
use JSON::XS;
use Backblaze::B2;
use Getopt::Long;

GetOptions(
    'c|credentials:s' => \my $credentials_file,
);

my ($bucket_name, @files) = @ARGV;

=head1 SYNOPSIS

=cut

my $b2 = Backblaze::B2->new(
    version => 'v1',
    log_message => sub { warn sprintf "[%d] %s\n", @_; },
);

my $credentials = $b2->read_credentials( $credentials_file );
if( ! $credentials->{authorizationToken}) {
    $b2->authorize_account(%$credentials);
};

(my $bucket) = grep { $_->name =~ /$bucket_name/ or $_->id eq $bucket_name }
               sort { $a->name cmp $b->name }
               $b2->buckets;

if( ! $bucket ) {
    die "No bucket found with name matching '$bucket_name'";
};

lib/Backblaze/B2.pm  view on Meta::CPAN

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

lib/Backblaze/B2.pm  view on Meta::CPAN

        $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 ) = @_;
    

lib/Backblaze/B2/v1/AnyEvent.pm  view on Meta::CPAN

    bless \%options => $class;
}

sub log_message {
    my( $self ) = shift;
    if( $self->{log_message}) {
        goto &{ $self->{log_message}};
    };
}

sub read_credentials {
    my( $self, $file ) = @_;
    
    if( ! defined $file) {
        require File::HomeDir;
        $file = File::HomeDir->my_home . "/credentials.b2";
        $self->log_message(0, "Using default credentials file '$file'");
    };
    
    $self->log_message(1, "Reading credentials from '$file'");
    
    open my $fh, '<', $file
        or croak "Couldn't read credentials from '$file': $!";
    binmode $fh;
    local $/;
    my $json = <$fh>;
    my $cred = decode_json( $json );
    
    $self->{credentials} = $cred;
    
    $cred
};

sub decode_json_response {
    my($self, $body,$hdr) = @_;
    
    $self->log_message(1, sprintf "HTTP Response status %d", $hdr->{Status});

    my @result;

lib/Backblaze/B2/v1/AnyEvent.pm  view on Meta::CPAN

            $self->log_message(4, sprintf "HTTP error status: %s: %s", $status, $reason);
            @result = ( 0, sprintf(sprintf "HTTP error status: %s: %s", $status, $reason));
        } else {
            @result = (1, "", $b);
        };
    };
    
    @result
}

# Provide headers from the credentials, if available
sub get_headers {
    my( $self ) = @_;
    if( my $token = $self->authorizationToken ) {
        return Authorization => $token
    };
    return ()
}

sub accountId {
    my( $self ) = @_;
    $self->{credentials}->{accountId}
}

sub authorizationToken {
    my( $self ) = @_;
    $self->{credentials}->{authorizationToken}
}

sub downloadUrl {
    my( $self ) = @_;
    $self->{credentials}->{downloadUrl}
}

sub apiUrl {
    my( $self ) = @_;
    $self->{credentials}->{apiUrl}
}


=head2 C<< ->request >>

Returns a promise that will resolve to the response data and the headers from
the request.

=cut

lib/Backblaze/B2/v1/AnyEvent.pm  view on Meta::CPAN

        url => $url,
        headers => {
            "Authorization" => "Basic $auth"
        },
    )->then( sub {
        my( $ok, $msg, $cred ) = @_;
        
        if( $ok ) {
            $self->log_message(1, sprintf "Storing authorization token");
            
            $self->{credentials} = $cred;
        };
        
        return ( $ok, $msg, $cred );
    });
}

=head2 C<< $b2->create_bucket >>

  $b2->create_bucket(
      bucketName => 'my_files',

lib/Backblaze/B2/v1/AnyEvent.pm  view on Meta::CPAN

=cut

sub download_file_by_name {
    my( $self, %options ) = @_;
    
    croak "Need a bucket name"
        unless defined $options{ bucketName };
    croak "Need a file name"
        unless defined $options{ fileName };
    my $url = join '/',
        $self->{credentials}->{downloadUrl},
        'file',
        delete $options{ bucketName },
        delete $options{ fileName }
        ;
    $self->log_message(1, sprintf "Fetching %s", $url );

    $self->request(
        url => $url,
        %options
    )->then(sub {

lib/Backblaze/B2/v1/Synchronous.pm  view on Meta::CPAN

        require Backblaze::B2::v1::AnyEvent;
        Backblaze::B2::v1::AnyEvent->new(
            api_base => $Backblaze::B2::v1::API_BASE,
            %options
        );
    };
    
    bless \%options => $class;
}

sub read_credentials {
    my( $self, @args ) = @_;
    $self->api->read_credentials(@args)
}

sub downloadUrl { $_[0]->api->downloadUrl };
sub apiUrl { $_[0]->api->apiUrl };

sub await($) {
    my $promise = $_[0];
    my @res;
    if( $promise->is_unfulfilled ) {
        require AnyEvent;

t/50-interactive-public-file.t  view on Meta::CPAN

#!perl -w
use strict;
use JSON::XS;
use Backblaze::B2;
use Test::More;
use Getopt::Long;

GetOptions(
    'o|application-credentials:s' => \my $app_credentials_file,
);

if( ! $app_credentials_file) {
    $app_credentials_file ||= $ENV{B2_CREDENTIALS_FILE};
};

$app_credentials_file ||= './app-credentials.json';

{
    if( !-f $app_credentials_file ) {;
        SKIP: {
            skip sprintf('No app_credentials read from %s; set $ENV{B2_CREDENTIALS_FILE} for interactive tests', $app_credentials_file),1
        }
        done_testing;
        exit;
    };
}

my $bucket_name = 'backblaze-b2-test-bucket';

my $b2 = Backblaze::B2->new(
    version => 'v1',
    log_message => sub { diag sprintf "[%d] %s\n", @_; },
);

my $credentials = $b2->read_credentials( $app_credentials_file );
if( ! $credentials->{authorizationToken}) {
    $b2->authorize_account(%$credentials);
};

ok $b2, "Authorizing works";

(my $bucket) = grep { $_->name =~ /$bucket_name/ or $_->id eq $bucket_name }
               sort { $a->name cmp $b->name }
               $b2->buckets;

if( ! $bucket) {
    diag "No bucket with name '$bucket_name' found, creating";



( run in 0.366 second using v1.01-cache-2.11-cpan-4d50c553e7e )