Skip to main content

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

Project description

aiob2


Python version Build status 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+ (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, Client

# 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():
    client = Client(conn_info)
    file = await client.upload_file(
        content_bytes=data,
        content_type='image/jpeg',
        file_name='test.jpg',
        bucket_id='bucket_id',
    )
    await client.close()


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
- id: str
- info: dict
- name: str
- 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
deleted_file = await client.delete_file(file_name='file_name', file_id='file_id')

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

- name: str
- id: str

Downloading

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

downloaded_file = await client.download_file_by_name(file_name='file_name', bucket_name='bucket_name')
downloaded_file = await client.download_file_by_id(file_id='file_id')

This will return a DownloadedFile object with the following attributes:

- name: str
- 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.5.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiob2-0.5.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file aiob2-0.5.0.tar.gz.

File metadata

  • Download URL: aiob2-0.5.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Windows/10

File hashes

Hashes for aiob2-0.5.0.tar.gz
Algorithm Hash digest
SHA256 19d416902a11da3a73288a0c1b56af7c6c514cfa47b528419ab657ec25d22674
MD5 20a4c42e4e2f6e093dd409afa31eac71
BLAKE2b-256 edfc52273667797f1aa77535e2a665606e51877847b572b5087160e910b23c82

See more details on using hashes here.

File details

Details for the file aiob2-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: aiob2-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Windows/10

File hashes

Hashes for aiob2-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e74502f6ece30ab3f5acbe5ca40834f0acb3bf7109a8340749a4a5a742fefe4e
MD5 8958123c433924dd68967afe9a1c622b
BLAKE2b-256 f4ce7489249dc13f4cabc1ce70049dd5795f7f08aa494fa1c417d01ccfa6c61b

See more details on using hashes here.

Supported by

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