Skip to content

Instantly share code, notes, and snippets.

@aruseni
Created June 2, 2025 11:34
Show Gist options
  • Select an option

  • Save aruseni/a1177db0bd331ee27b6b576a50346005 to your computer and use it in GitHub Desktop.

Select an option

Save aruseni/a1177db0bd331ee27b6b576a50346005 to your computer and use it in GitHub Desktop.
Update the content type of all .avif files in a B2 bucket to image/avif
import os
from b2sdk.v2 import B2Api, InMemoryAccountInfo
B2_APPLICATION_KEY_ID = ""
B2_APPLICATION_KEY = ""
B2_BUCKET_NAME = ""
info = InMemoryAccountInfo()
b2_api = B2Api(info)
b2_api.authorize_account(
"production", B2_APPLICATION_KEY_ID, B2_APPLICATION_KEY
)
bucket = b2_api.get_bucket_by_name(B2_BUCKET_NAME)
for file_version, _ in bucket.ls("", recursive="True"):
if file_version.file_name.endswith('.avif'):
print(f"{file_version.file_name} -> {file_version.content_type}")
# Step 1: Download the file
local_path = os.path.join("bucket_files", file_version.file_name)
os.makedirs(os.path.dirname(local_path), exist_ok=True)
bucket.download_file_by_id(file_version.id_).save_to(local_path)
# Step 2: Delete the specific file version
bucket.delete_file_version(file_version.id_, file_version.file_name)
# Step 3: Re-upload with correct MIME type
bucket.upload_local_file(
local_file=local_path,
file_name=file_version.file_name,
content_type="image/avif"
)
print(f"Re-uploaded with image/avif MIME: {file_version.file_name}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment