Last active
January 16, 2025 07:27
-
-
Save nilayp/2c2a04f033d8992ce4b8f591ab44982e to your computer and use it in GitHub Desktop.
Using presigned URLs with Backblaze B2 + AWS Python SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
''' | |
Backblaze wants developers and organization to copy and re-use our | |
code examples, so we make the samples available by several different | |
licenses. One option is the MIT license (below). Other options are | |
available here: | |
https://www.backblaze.com/using_b2_code.html | |
The MIT License (MIT) | |
Copyright (c) 2021 Backblaze | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
''' | |
# Using presigned URLs with Backblaze B2 + AWS Python SDK | |
import requests | |
import boto3 | |
from botocore.config import Config | |
# Specify the B2 endpoint for the bucket. | |
# This can be found here: https://secure.backblaze.com/b2_buckets.htm | |
region= 'us-west-000' | |
endpoint = f'https://s3.{region}.backblazeb2.com' | |
bucket_name = 'mybucketgoeshere' | |
# Specify the B2 application key. | |
# This can be found here: https://secure.backblaze.com/app_keys.htm | |
# Create a new key and ensure that "Allow List All Bucket Names" is checked. | |
app_key_id = '000862xxxxxxxxxxxxxx' | |
app_key_sec = 'K000xxxxxxxxxxxxxxxxxxx' | |
key_name = 'puppy.jpeg' | |
# Create a boto3.client using AWS v4 authentication. Backblaze B2 does not | |
# support v2 authentication as it has been deprecated by AWS. | |
s3 = boto3.client('s3', | |
endpoint_url=endpoint, | |
aws_access_key_id=app_key_id, | |
aws_secret_access_key=app_key_sec, | |
region_name=region, | |
config=Config(signature_version = 's3v4')) | |
params = {'Bucket': bucket_name, 'Key': key_name} | |
# Ensure the presigned URL allows the use of HTTP PUT method. Backblaze B2 | |
# does not currently support HTTP POST. | |
presigned_url = s3.generate_presigned_url("put_object", Params=params, ExpiresIn=30) | |
with open(key_name, "rb") as file_to_upload: | |
put_response = requests.put(presigned_url, data = file_to_upload) | |
print(put_response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment