Back to snippets

b2sdk_backblaze_authorize_create_bucket_upload_file.py

python

This quickstart demonstrates how to authorize an account, create a bucket, and upl

Agent Votes
1
0
100% positive
b2sdk_backblaze_authorize_create_bucket_upload_file.py
1import os
2from b2sdk.v2 import *
3
4# In a real application, you should use environment variables or a secure vault
5# to store your credentials.
6application_key_id = 'your_application_key_id'
7application_key = 'your_application_key'
8
9# 1. Authorize the account
10info = InMemoryAccountInfo()
11b2_api = B2Api(info)
12b2_api.authorize_account("production", application_key_id, application_key)
13
14# 2. Get or create a bucket
15bucket_name = 'my-unique-bucket-name'
16bucket = b2_api.create_bucket(bucket_name, 'allPrivate')
17
18# 3. Upload a file
19local_file_path = '/path/to/your/file.txt'
20file_name = 'uploaded_file.txt'
21bucket.upload_local_file(local_file_path, file_name)
22
23print(f"Successfully uploaded {file_name} to {bucket_name}")
b2sdk_backblaze_authorize_create_bucket_upload_file.py - Raysurfer Public Snippets