Backblaze-B2V4
view release on metacpan or search on metacpan
script/b2_client view on Meta::CPAN
# b2_client file_info FILE_ID
Displays lots of information about a given file. See https://www.backblaze.com/apidocs/b2-get-file-info
# b2_client new_bucket BUCKET_NAME
Creates a new B2 Bucket with the given name. Alphanumeric characters only.
Note that B2 bucket namess must be unique system-wide, not just your account. Select a name that willbe unique globally.
# b2_client help
Prints this help screen.
--
Backblaze::B2V4 and this command are released under the MIT License. Copyright (c) 2026 Eric Chernoff
};
}
# Next two subroutines will retrieve or save their B2 API tokens
sub get_b2_tokens {
my ($obfuscated_tokens, $the_tokens, $application_key_id, $application_key);
# try to read it in
eval {
$obfuscated_tokens = path('~/.b2_tokens')->slurp_raw;
$the_tokens = pack "h*", $obfuscated_tokens;
($application_key_id, $application_key) = split /:::/, $the_tokens;
};
if ($@ || !$application_key_id || !$application_key) {
say "ERROR: B2 tokens not available.".
"\nPlease use 'b2_client save_tokens APPLICATION_KEY_ID APPLICATION_KEY' to save tokens to your home directory.";
return;
}
return [
$application_key_id,
$application_key
];
}
sub save_b2_tokens {
my (@args) = @_;
if (!$ARGV[0] || !$ARGV[1]) {
return "ERROR: Please provide both the APPLICATION_KEY_ID and APPLICATION_KEY arguments for 'save_tokens'\n";
}
# test them before saving
my $b2 = get_b2_session([ $args[0], $args[1] ]);
# get_b2_session() will test those for us
my $obfuscated_content = unpack "h*", $args[0].':::'.$args[1];
my $file_location = '~/.b2_tokens';
path($file_location)->spew_raw( $obfuscated_content );
chmod 0600, $file_location;
return "B2 API Tokens saved to $file_location\n";
}
# subroutine to actually log into B2 with their tokens
sub get_b2_session {
my ($tokens) = @_;
# if no tokens provided, try to load them
if (!$$tokens[0]) {
$tokens = get_b2_tokens();
}
# no tokens? prompt them to create
die "API tokens needed for get_b2_session()\nPlease run 'b2_client save_tokens'.\n" if !$$tokens[0] || !$$tokens[1];
# attempt to log in
my $b2 = Backblaze::B2V4->new(
application_key_id => $$tokens[0],
application_key => $$tokens[1]
);
if ($b2->login_error) {
die "ERROR: The B2 API keys you provided did not authenticate. Please verify and try again.\n";
}
return $b2;
}
sub show_result {
my ($b2, $result_message) = @_;
if ($b2->current_status_is_not_ok) {
return get_error_message($b2);
}
return $result_message . "\n";
}
sub get_error_message {
my ($b2) = @_;
if ($b2->latest_error() !~ /^Error/) {
return "Error: " . $b2->latest_error() . "\n";
}
return $b2->latest_error() . "\n";
}
( run in 1.277 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )