Backblaze-B2

 view release on metacpan or  search on metacpan

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

    }
}

=head2 C<< ->bucket_from_id >>

    my @buckets = $b2->bucket_from_id(
        'deadbeef'
    );

Returns a L<Backblaze::B2::Bucket> object that has the given ID. It
does not make an HTTP request to fetch the name and status of that bucket.

=cut

sub bucket_from_id {
    my( $self, $bucket_id ) = @_;
    $self->_new_bucket( bucketId => $bucket_id );
}

=head2 C<< ->create_bucket >>

    my $new_bucket = $b2->create_bucket(
        name => 'my-new-bucket', # only /[A-Za-z0-9-]/i are allowed as bucket names
        type => 'allPrivate', # or allPublic
    );
    
    print sprintf "Created new bucket %s\n", $new_bucket->id;

Creates a new bucket and returns it.

=cut

sub create_bucket {
    my( $self, %options ) = @_;
    $options{ type } ||= 'allPrivate';
    my $b = $self->api->asyncApi->create_bucket(
        bucketName => $options{ name },
        bucketType => $options{ type },
    )->then( sub {
        my( $bucket ) = @_;
        $self->_new_bucket( %$bucket );
    });

    if( !$self->api->isAsync ) {
        Backblaze::B2::v1::payload $b
    }
}

=head2 C<< ->api >>

Returns the underlying API object

=cut

sub api { $_[0]->{api} }

1;

package Backblaze::B2::v1::Bucket;
use strict;
use Scalar::Util 'weaken';

sub new {
    my( $class, %options ) = @_;
    weaken $options{ parent };
    
    # Whoa! We assume that the async version has the same class name
    # as the synchronous version and just strip it off.
    $options{ file_class } =~ s!::Synchronized$!!;
    
    bless \%options => $class,
}

sub name { $_[0]->{bucketName} }
#sub api { $_[0]->{api} }
sub downloadUrl { join "/", $_[0]->api->downloadUrl, $_[0]->name }
sub id { $_[0]->{bucketId} }
sub type { $_[0]->{bucketType} }
sub account { $_[0]->{parent} }

sub _new_file {
    my( $self, %options ) = @_;
    # Should this one magically unwrap AnyEvent::condvar objects?!
    
    #warn $self->{file_class};
    #use Data::Dumper;
    #warn Dumper \%options;
    
    $self->{file_class}->new(
        %options,
        api => $self->api,
        bucket => $self
    );
}

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

Lists the files contained in this bucket

    my @files = $bucket->files(
        startFileName => undef,
    );

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

=over 4

=item C<< allFiles >>

    allFiles => 1

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

=back

=cut

sub files {
    my( $self, %options ) = @_;
    $options{ maxFileCount } ||= 1000;
    #$options{ startFileName } ||= undef;
    
    $self->api->asyncApi->list_all_file_names(

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


=item C<< mime_type >>

Content-type of the stored file. Defaults to autodetection by the B2 API.

=item C<< content >>

If you don't have the local content in a file on disk, you can
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',
    );

This saves you searching through the list of existing files
if you already know the filename.

=cut

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

=head2 C<< ->api >>

Returns the underlying API object

=cut

sub api { $_[0]->{api} }

package Backblaze::B2::v1::File;
use strict;
#use Scalar::Util 'weaken'; # do we really want to weaken our link?!
# The bucket doesn't hold a ref to us, so we don't want to weaken it

sub new {
    my( $class, %options ) = @_;
    #weaken $options{ bucket };
    #warn "$class: " . join ",", sort keys %options;
    
    bless \%options => $class,
}

sub name { $_[0]->{fileName} }
sub id { $_[0]->{fileId} }
sub action { $_[0]->{action} }
sub bucket { $_[0]->{bucket} }
sub size { $_[0]->{size} }
sub downloadUrl { join "/", $_[0]->bucket->downloadUrl, $_[0]->name }

package Backblaze::B2::v1::File::Synchronized;
use strict;
use Carp qw(croak);
#use Scalar::Util 'weaken'; # do we really want to weaken our link?!
# The bucket doesn't hold a ref to us, so we don't want to weaken it

sub new {
    my( $class, %options ) = @_;
    #weaken $options{ bucket };
    #warn "$class: " . join ",", sort keys %options;
    croak "Need impl" unless $options{ impl };
    
    bless \%options => $class,
}

sub name { $_[0]->{impl}->name }
sub id { $_[0]->{impl}->id }
sub action { $_[0]->{impl}->action }
sub bucket { $_[0]->{impl}->bucket }
sub size { $_[0]->{impl}->size }
sub downloadUrl { $_[0]->{impl}->downloadUrl }


1;



( run in 1.895 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )