Backblaze-B2V2Client
view release on metacpan or search on metacpan
my $operation_status = $b2client->b2_upload_file(
'bucket_name' => 'GingerAnna',
'new_file_name' => 'ginger_was_perfect.jpg',
'file_contents' => $file_contents
);
NOTE: If you are going to use the 'file\_contents' method, it's best
to load the scalar using the 'slurp\_raw' method in Path::Tiny.
(I believe 'read\_file' in File::Slurp will work, but have yet to test.)
You can also pass a 'content-type' key with the MIME type for the new
file. The default is 'b2/auto'.
Upon a successful upload, the new GUID for the file will be available
in $b2client->{b2\_response}{fileId} .
See: https://www.backblaze.com/b2/docs/b2\_upload\_file.html
## b2\_upload\_large\_file
Uploads a large file into B2. Recommended for uploading files larger
lib/Backblaze/B2V2Client.pm view on Meta::CPAN
# method to upload a file into Backblaze B2
sub b2_upload_file {
my $self = shift;
my (%args) = @_;
# this must include valid entries for 'new_file_name' and 'bucket_name'
# and it has to include either the raw file contents in 'file_contents'
# or a valid location in 'file_location'
# also, you can include 'content_type' (which would be the MIME Type'
# if you do not want B2 to auto-determine the MIME/content-type
# did they provide a file location or path?
if ($args{file_location} && -e "$args{file_location}") {
$args{file_contents} = path( $args{file_location} )->slurp_raw;
# if they didn't provide a file-name, use the one on this file
$args{new_file_name} = path( $args{file_location} )->basename;
}
# were these file contents either provided or found?
lib/Backblaze/B2V2Client.pm view on Meta::CPAN
$self->error_tracker(qq{You must provide either a valid 'file_location' or 'file_contents' arg for b2_upload_file().});
return 'Error';
}
# check the other needed args
if (!$args{bucket_name} || !$args{new_file_name}) {
$self->error_tracker(qq{You must provide 'bucket_name' and 'new_file_name' args for b2_upload_file().});
return 'Error';
}
# default content-type
$args{content_type} ||= 'b2/x-auto';
# OK, let's continue: get the upload URL and authorization token for this bucket
$self->b2_get_upload_url( $args{bucket_name} );
# send the special request
$self->b2_talker(
'url' => $self->{bucket_info}{ $args{bucket_name} }{upload_url},
'authorization' => $self->{bucket_info}{ $args{bucket_name} }{authorization_token},
'file_contents' => $args{file_contents},
lib/Backblaze/B2V2Client.pm view on Meta::CPAN
}
# method to upload a large file (>100MB)
sub b2_upload_large_file {
my $self = shift;
my (%args) = @_;
# this must include valid entries for 'new_file_name' and 'bucket_name'
# and it has to a valid location in 'file_location' (Do not load in file contents)
# also, you can include 'content_type' (which would be the MIME Type'
# if you do not want B2 to auto-determine the MIME/content-type
# did they provide a file location or path?
if ($args{file_location} && -e "$args{file_location}") {
# if they didn't provide a file-name, use the one on this file
$args{new_file_name} = path( $args{file_location} )->basename;
} else {
$self->error_tracker(qq{You must provide a valid 'file_location' arg for b2_upload_large_file().});
return $self->{current_status};
}
lib/Backblaze/B2V2Client.pm view on Meta::CPAN
$self->error_tracker(qq{Please use b2_upload_large_file() for files larger than $self->{recommended_part_size} .});
return $self->{current_status};
}
# need a bucket name
if (!$bucket_name) {
$self->error_tracker(qq{You must provide a valid 'bucket_name' arg for b2_upload_large_file().});
return $self->{current_status};
}
# default content-type
$args{content_type} ||= 'b2/x-auto';
# get the bucket ID
$self->b2_list_buckets($bucket_name);
# kick off the upload in the API
$self->b2_talker(
'url' => $self->{api_url}.'/b2api/v2/b2_start_large_file',
'authorization' => $self->{account_authorization_token},
'post_params' => {
lib/Backblaze/B2V2Client.pm view on Meta::CPAN
my $operation_status = $b2client->b2_upload_file(
'bucket_name' => 'GingerAnna',
'new_file_name' => 'ginger_was_perfect.jpg',
'file_contents' => $file_contents
);
NOTE: If you are going to use the 'file_contents' method, it's best
to load the scalar using the 'slurp_raw' method in Path::Tiny.
(I believe 'read_file' in File::Slurp will work, but have yet to test.)
You can also pass a 'content-type' key with the MIME type for the new
file. The default is 'b2/auto'.
Upon a successful upload, the new GUID for the file will be available
in $b2client->{b2_response}{fileId} .
See: https://www.backblaze.com/b2/docs/b2_upload_file.html
=head2 b2_upload_large_file
Uploads a large file into B2. Recommended for uploading files larger
( run in 1.987 second using v1.01-cache-2.11-cpan-524268b4103 )