Skip to content

Instantly share code, notes, and snippets.

@sun0225SUN
Last active August 31, 2025 12:37
Show Gist options
  • Select an option

  • Save sun0225SUN/b043617c1949d09b72781e06fc3387d8 to your computer and use it in GitHub Desktop.

Select an option

Save sun0225SUN/b043617c1949d09b72781e06fc3387d8 to your computer and use it in GitHub Desktop.
Download all files from the CloudFlare bucket.
import boto3
import os
# 显式设置R2的访问凭证和配置
s3_client = boto3.client(
's3',
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
endpoint_url='https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com',
region_name='auto'
)
# R2存储桶名称
bucket_name = 'your-bucket-name'
# 本地存储路径
local_dir = '/path/to/local/directory'
def download_files(bucket_name, local_dir):
# 列出存储桶中的所有对象
response = s3_client.list_objects_v2(Bucket=bucket_name)
if 'Contents' in response:
for obj in response['Contents']:
file_key = obj['Key']
local_file_path = os.path.join(local_dir, file_key)
# 创建本地目录
os.makedirs(os.path.dirname(local_file_path), exist_ok=True)
# 下载文件
s3_client.download_file(bucket_name, file_key, local_file_path)
print(f'Downloaded {file_key} to {local_file_path}')
else:
print('No files found in the bucket.')
# 调用下载函数
download_files(bucket_name, local_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment