Backblaze-B2

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

.gitignore
Changes
examples/authenticate.pl
examples/create-bucket.pl
examples/delete-bucket.pl
examples/download-file.pl
examples/list-bucket-contents.pl
examples/list-buckets.pl
examples/upload-file-async.pl
examples/upload-file.pl
lib/Backblaze/B2.pm
lib/Backblaze/B2/v1/AnyEvent.pm
lib/Backblaze/B2/v1/Synchronous.pm
Makefile.PL
MANIFEST			This list of files
MANIFEST.SKIP
README
README.mkdn
t/00-load.t
t/50-interactive-public-file.t

README  view on Meta::CPAN

    By default it returns only the first 1000 files, but see the allFiles
    parameter.

    allFiles

          allFiles => 1

      Passing in a true value for this parameter will make as many API
      calls as necessary to fetch all files.

 ->upload_file( %options )

    Uploads a file into this bucket, potentially creating a new file
    version.

        my $new_file = $bucket->upload_file(
            file => 'some/local/file.txt',
            target_file => 'the/public/name.txt',
        );

    file

      Local name of the source file. This file will be loaded into memory
      in one go.

    target_file

README  view on Meta::CPAN

      the content in as a string.

    mtime

      Time in miliseconds since the epoch to when the content was created.
      Defaults to the current time.

    sha1

      Hexdigest of the SHA1 of the content. If this is missing, the SHA1
      will be calculated upon upload.

 ->download_file_by_name( %options )

    Downloads a file from this bucket by name:

        my $content = $bucket->download_file_by_name(
            fileName => 'the/public/name.txt',
        );

    This saves you searching through the list of existing files if you

README  view on Meta::CPAN

    By default it returns only the first 1000 files, but see the allFiles
    parameter.

    allFiles

          allFiles => 1

      Passing in a true value for this parameter will make as many API
      calls as necessary to fetch all files.

 ->upload_file( %options )

    Uploads a file into this bucket, potentially creating a new file
    version.

        my $new_file = $bucket->upload_file(
            file => 'some/local/file.txt',
            target_file => 'the/public/name.txt',
        );

    file

      Local name of the source file. This file will be loaded into memory
      in one go.

    target_file

README  view on Meta::CPAN

      the content in as a string.

    mtime

      Time in miliseconds since the epoch to when the content was created.
      Defaults to the current time.

    sha1

      Hexdigest of the SHA1 of the content. If this is missing, the SHA1
      will be calculated upon upload.

 ->download_file_by_name( %options )

    Downloads a file from this bucket by name:

        my $content = $bucket->download_file_by_name(
            file => 'the/public/name.txt',
        );

    This saves you searching through the list of existing files if you

README.mkdn  view on Meta::CPAN

By default it returns only the first 1000
files, but see the `allFiles` parameter.

- `allFiles`

        allFiles => 1

    Passing in a true value for this parameter will make
    as many API calls as necessary to fetch all files.

## `->upload_file( %options )`

Uploads a file into this bucket, potentially creating
a new file version.

    my $new_file = $bucket->upload_file(
        file => 'some/local/file.txt',
        target_file => 'the/public/name.txt',
    );

- `file`

    Local name of the source file. This file will be loaded
    into memory in one go.

- `target_file`

README.mkdn  view on Meta::CPAN

    pass the content in as a string.

- `mtime`

    Time in miliseconds since the epoch to when the content was created.
    Defaults to the current time.

- `sha1`

    Hexdigest of the SHA1 of the content. If this is missing, the SHA1
    will be calculated upon upload.

## `->download_file_by_name( %options )`

Downloads a file from this bucket by name:

    my $content = $bucket->download_file_by_name(
        fileName => 'the/public/name.txt',
    );

This saves you searching through the list of existing files

README.mkdn  view on Meta::CPAN

By default it returns only the first 1000
files, but see the `allFiles` parameter.

- `allFiles`

        allFiles => 1

    Passing in a true value for this parameter will make
    as many API calls as necessary to fetch all files.

## `->upload_file( %options )`

Uploads a file into this bucket, potentially creating
a new file version.

    my $new_file = $bucket->upload_file(
        file => 'some/local/file.txt',
        target_file => 'the/public/name.txt',
    );

- `file`

    Local name of the source file. This file will be loaded
    into memory in one go.

- `target_file`

README.mkdn  view on Meta::CPAN

    pass the content in as a string.

- `mtime`

    Time in miliseconds since the epoch to when the content was created.
    Defaults to the current time.

- `sha1`

    Hexdigest of the SHA1 of the content. If this is missing, the SHA1
    will be calculated upon upload.

## `->download_file_by_name( %options )`

Downloads a file from this bucket by name:

    my $content = $bucket->download_file_by_name(
        file => 'the/public/name.txt',
    );

This saves you searching through the list of existing files

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

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,
        )->then(sub {
            my( $res ) = @_;
            print "$file uploaded\n";
        })->catch(sub {
            warn "@_"
        });
    } @files );

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

(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'";
};

print sprintf "Uploading to bucket %s\n", $bucket->name;
for my $file (@files) {
    my $f = $bucket->upload_file(
        file => $file,
    );
    print sprintf "Uploaded %s\n", $f->name;
};

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

    $self->api->asyncApi->list_all_file_names(
        bucketId => $self->id,
        %options,
    )->then( sub {
        my( $ok, $msg, @res ) = @_;
        
        $ok, $msg, map { $self->_new_file( %$_, bucket => $self ) } @res
    })
}

=head2 C<< ->upload_file( %options ) >>

Uploads a file into this bucket, potentially creating
a new file version.

    my $new_file = $bucket->upload_file(
        file => 'some/local/file.txt',
        target_file => 'the/public/name.txt',
    );

=over 4

=item C<< file >>

Local name of the source file. This file will be loaded
into memory in one go.

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

pass the content in as a string.

=item C<< mtime >>

Time in miliseconds since the epoch to when the content was created.
Defaults to the current time.

=item C<< sha1 >>

Hexdigest of the SHA1 of the content. If this is missing, the SHA1
will be calculated upon upload.

=back

=cut

sub upload_file {
    my( $self, %options ) = @_;
    
    my $api = $self->api->asyncApi;
    $api->get_upload_url(
        bucketId => $self->id,
    )->then(sub {
        my( $ok, $msg, $upload_handle ) = @_;
        $api->upload_file(
            %options,
            handle => $upload_handle
        );
    })->then( sub {
        my( $ok, $msg, @res ) = @_;

        (my $res) = map { $self->_new_file( %$_, bucket => $self ) } @res;
        $ok, $msg, $res
    });
}

=head2 C<< ->download_file_by_name( %options ) >>

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

=back

=cut

sub files {
    my( $self, %options ) = @_;
    map { $self->_new_file( impl => $_ ) }
        Backblaze::B2::v1::payload $self->impl->files( %options );
}

=head2 C<< ->upload_file( %options ) >>

Uploads a file into this bucket, potentially creating
a new file version.

    my $new_file = $bucket->upload_file(
        file => 'some/local/file.txt',
        target_file => 'the/public/name.txt',
    );

=over 4

=item C<< file >>

Local name of the source file. This file will be loaded
into memory in one go.

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

pass the content in as a string.

=item C<< mtime >>

Time in miliseconds since the epoch to when the content was created.
Defaults to the current time.

=item C<< sha1 >>

Hexdigest of the SHA1 of the content. If this is missing, the SHA1
will be calculated upon upload.

=back

=cut

sub upload_file {
    my( $self, %options ) = @_;

    Backblaze::B2::v1::payload $self->impl->upload_file( %options );
}

=head2 C<< ->download_file_by_name( %options ) >>

Downloads a file from this bucket by name:

    my $content = $bucket->download_file_by_name(
        file => 'the/public/name.txt',
    );

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

    my( $self, %options ) = @_;
    
    $options{ accountId } ||= $self->accountId;
    
    $self->json_request(api_endpoint => 'b2_list_buckets',
        accountId => $options{ accountId },
        %options
    )
}

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

  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;

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

    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

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

               $b2->buckets;

if( ! $bucket) {
    diag "No bucket with name '$bucket_name' found, creating";
    $bucket = $b2->create_bucket(name => $bucket_name,
                                 type => 'allPublic' );
    ok $bucket, "We created a (public) bucket: " . $bucket->name;
};

diag "Uploading $0 to public test bucket";
my $f = $bucket->upload_file(
    file => $0,
    target_name => 'my_file.t',
);

use Data::Dumper;
diag "Upload is reachable as " . $f->name;
diag "Upload is reachable as " . $f->downloadUrl;

my $fetch_as = $f->name;
$fetch_as =~ s!\\!/!g; # just in case we ran on Windows



( run in 3.204 seconds using v1.01-cache-2.11-cpan-b16cb0d3907 )