Skip to main content

A simple and easy to use async wrapper for Backblaze's B2 bucket API.

Project description

aiob2


Python Version PyPi License

aiob2 is an asynchronous API wrapper for the Backblaze B2 Bucket API.

It will allow you to interact with your B2 bucket, it's files and anything else that the B2 API allows in a modern, object-oriented fashion.

NOTE: This API wrapper is by no means complete and has many endpoints to cover, though the main ones have been covered (they will be listed below)

Installation


aiob2 is compatible with Python 3.8.10+ (this is an estimate). To install aiob2, run the following command in your (virtual) environment.

pip install aiob2

Alternatively, for the latest though least stable version, you can download it from the GitHub repo:

pip install git+https://github.com/Void-ux/aiob2.git

Usage

Uploading

import aiohttp
import asyncio

from aiob2 import B2ConnectionInfo, bucket

# Construct our connection info
conn_info = B2ConnectionInfo('key_id', 'app_id')

# Our image to upload to our bucket
with open(r'C:\Users\MS1\Pictures\Camera Roll\IMG_5316.jpeg', 'rb') as file:
    data = file.read()

async def main():
    async with aiohttp.ClientSession() as s:
        await bucket.upload_file(
            content_bytes=data,
            content_type='image/jpeg',
            file_name='home.jpeg',
            session=s,
            bucket_id='bucket_id',
            connection_info=conn_info
        )


if __name__ == '__main__':
    asyncio.run(main())

And that's it! upload_file() returns a File object that neatly wraps everything Backblaze's API has provided us with. The File object has the following attributes:

- account_id: str
- action: str
- bucket_id: str
- content_length: int
- content_sha1: str
- content_md5: str
- content_type: str
- file_id: str
- file_info: dict
- file_name: str
- file_retention: dict
- legal_hold: dict
- server_side_encryption: dict
- upload_timestamp: datetime.datetime

You can visit the bucket.py file to view the source code of this class.

Deleting

# We can remove the boilerplate code and get straight to the method
from aiob2 import bucket

await bucket.delete_file(
    file_name='home.jpeg',
    file_id='4_z275c6d8d808e543872cc0215_f11088ad8814ee120_d20220514_m211709_c002_v0001096_t0019_u01652563029709',
    connection_info=conn_info,
    session=s
)

This will return a DeletedFile object, it has the following attributes:

- file_name: str
- file_id: str

Downloading

Downloading a file can be done either with the name or the id of it.

# We can remove the boilerplate code and get straight to the method
from aiob2 import bucket

await bucket.download_file_by_name(
    file_name='home.jpeg',
    bucket_name='foo',
    connection_info=conn_info,
    session=s
)
from aiob2 import bucket

await bucket.download_file_by_id(
    file_id='home.jpeg',
    connection_info=conn_info,
    session=s
)

This will return a DownloadedFile object with the following attributes:

- file_name: str
- file_id: str
- content_sha1: str
- upload_timestamp: datetime.datetime
- accept_ranges: str
- content: bytes
- content_type: str
- content_length: str
- date: str

NOTE: There are many kwargs you can provide when downloading a file, it's recommended to take a look at the source code to see if any can benefit you and your usecase.

License


This project is released under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiob2-0.2.1.tar.gz (8.4 kB view hashes)

Uploaded Source

Built Distribution

aiob2-0.2.1-py3-none-any.whl (8.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page