Backblaze-B2V2Client
view release on metacpan or search on metacpan
lib/Backblaze/B2V2Client.pm view on Meta::CPAN
package Backblaze::B2V2Client;
# API client library for V2 of the API to Backblaze B2 object storage
# Allows for creating/deleting buckets, listing files in buckets, and uploading/downloading files
$Backblaze::B2V2Client::VERSION = '1.7';
# our dependencies:
use Cpanel::JSON::XS;
use Digest::SHA qw(sha1_hex);
use MIME::Base64;
use Path::Tiny;
use URI::Escape;
use WWW::Mechanize;
# I wish I could apply this to my diet.
use strict;
use warnings;
# object constructor; will automatically authorize this session
sub new {
my $class = shift;
# required args are the account ID and application_key
my ($application_key_id, $application_key) = @_;
# cannot operate without these
if (!$application_key_id || !$application_key) {
die "ERROR: Cannot create B2V5Client object without both application_key_id and application_key arguments.\n";
}
# initiate class with my keys + WWW::Mechanize object
my $self = bless {
'application_key_id' => $application_key_id,
'application_key' => $application_key,
'mech' => WWW::Mechanize->new(
timeout => 60,
autocheck => 0,
cookie_jar => {},
keep_alive => 1,
),
}, $class;
# now start our B2 session via method below
$self->b2_authorize_account(); # this adds more goodness to $self for use in the other methods
return $self;
}
# method to start your backblaze session: authorize the account and get your api URL's
sub b2_authorize_account {
my $self = shift;
# prepare our authorization header
my $encoded_auth_string = encode_base64($self->{application_key_id}.':'.$self->{application_key});
# add that header in
$self->{mech}->add_header( 'Authorization' => 'Basic '.$encoded_auth_string );
# call the b2_talker() method to authenticate our session
$self->b2_talker('url' => 'https://api.backblazeb2.com/b2api/v2/b2_authorize_account' );
# if we succeeded, load in our authentication and prepare to proceed
if ($self->{current_status} eq 'OK') {
$self->{account_id} = $self->{b2_response}{accountId};
$self->{api_url} = $self->{b2_response}{apiUrl};
$self->{account_authorization_token} = $self->{b2_response}{authorizationToken};
$self->{download_url} = $self->{b2_response}{downloadUrl};
# for uploading large files
$self->{recommended_part_size} = $self->{b2_response}{recommendedPartSize} || 104857600;
# ready!
# otherwise, not ready!
} else {
$self->{b2_login_error} = 1;
}
# return current status
return $self->{current_status};
}
# method to download a file by ID; probably most commonly used
sub b2_download_file_by_id {
my $self = shift;
# required arg is the file ID
# option arg is a target directory to auto-save the new file into
my ($file_id, $save_to_location) = @_;
if (!$file_id) {
$self->error_tracker('The file_id must be provided for b2_download_file_by_id().');
return $self->{current_status};
}
# send the request, as a GET
$self->b2_talker(
'url' => $self->{download_url}.'/b2api/v2/b2_download_file_by_id?fileId='.$file_id,
'authorization' => $self->{account_authorization_token},
( run in 2.240 seconds using v1.01-cache-2.11-cpan-df04353d9ac )