Backblaze-B2

 view release on metacpan or  search on metacpan

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

  my $upload_handle = $b2->get_upload_url();
  $b2->upload_file( file => $file, handle => $upload_handle );

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

=cut

sub get_upload_url {
    my( $self, %options ) = @_;
    
    croak "Need a bucketId"
        unless defined $options{ bucketId };

    $self->json_request(api_endpoint => 'b2_get_upload_url',
        %options
    )
}

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

  my $upload_handle = $b2->get_upload_url();
  $b2->upload_file(
      file => $file,
      handle => $upload_handle
  );

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

Note: This method loads the complete file to be uploaded
into memory.

Note: The Backblaze B2 API is vague about when you need
a new upload URL.

=cut

sub upload_file {
    my( $self, %options ) = @_;
    
    croak "Need an upload handle"
        unless defined $options{ handle };
    my $handle = delete $options{ handle };

    croak "Need a source file name"
        unless defined $options{ file };
    my $filename = delete $options{ file };
        
    my $target_filename = delete $options{ target_name };
    $target_filename ||= $filename;
    $target_filename =~ s!\\!/!g;
    $target_filename = encode('UTF-8', $target_filename );
    $target_filename =~ s!([^\x21-\x7d])!sprintf "%%%02x", ord $1!ge;
    
    my $mime_type = delete $options{ mime_type } || 'b2/x-auto';
    
    if( not defined $options{ content }) {
        open my $fh, '<', $filename
            or croak "Couldn't open '$filename': $!";
        binmode $fh, ':raw';
        $options{ content } = do { local $/; <$fh> }; # sluuuuurp
        $options{ mtime } = ((stat($fh))[9]) * 1000;
    };

    my $payload = delete $options{ content };
    if( not $options{ sha1 }) {
        my $sha1 = Digest::SHA1->new;
        $sha1->add( $payload );
        $options{ sha1 } = $sha1->hexdigest;
    };
    my $digest = delete $options{ sha1 };
    my $size = length($payload);
    my $mtime = delete $options{ mtime };

    $self->json_request(
        url => $handle->{uploadUrl},
        method => 'POST',
        _body => $payload,
        headers => {
            'Content-Type' => $mime_type,
            'Content-Length' => $size,
            'X-Bz-Content-Sha1' => $digest,
            'X-Bz-File-Name' => $target_filename,
            'Authorization' => $handle->{authorizationToken},
        },
        %options
    );
}

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

  my $startFileName;
  my $list = $b2->list_file_names(
      startFileName => $startFileName,
      maxFileCount => 1000, # maximum per round
      bucketId => ...,
      
  );

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

=cut

sub list_file_names {
    my( $self, %options ) = @_;
    
    croak "Need a bucket id"
        unless defined $options{ bucketId };

    $self->json_request(
        api_endpoint => 'b2_list_file_names',
        %options
    );
}

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

  my $list = $b2->list_all_file_names(
      startFileName => $startFileName,
      maxFileCount => 1000, # maximum per round
      bucketId => ...,
      



( run in 1.568 second using v1.01-cache-2.11-cpan-39bf76dae61 )