What is the name of the domain?
none
What is the issue you’re encountering
R2里面的文件无法管理
What steps have you taken to resolve the issue?
我尝试了使用boto3(python)来操作文件,但是一直提示ssl验证错误,我在想是否有更加简单和方便的办法,我只是想在同一个桶内移动文件,不应该如此复杂
What are the steps to reproduce the issue?
import boto3
from botocore.config import Config
from tqdm import tqdm
设置 R2 端点
s3 = boto3.client(
‘s3’,
endpoint_url=‘https://<ACCOUNT_ID>.r2.cloudflarestorage.com’,
aws_access_key_id=‘YOUR_ACCESS_KEY’,
aws_secret_access_key=‘YOUR_SECRET_KEY’,
config=Config(
signature_version=‘s3v4’,
region_name=‘auto’ # 关键!必须设置为 auto
)
)
bucket = ‘your_bucket_name’ # 替换为你的 S3 桶名称
source_dir = ‘source-folder/’ # 替换为你的源文件夹路径
target_dir = ‘target-folder/’ # 替换为你的目标文件夹路径
列出所有文件
paginator = s3.get_paginator(‘list_objects_v2’)
pages = paginator.paginate(Bucket=bucket, Prefix=source_dir)
获取所有文件键名
file_keys =
for page in pages:
for obj in page.get(‘Contents’, ):
file_keys.append(obj[‘Key’])
设置进度条
with tqdm(total=len(file_keys), desc=“Moving Files”, unit=“file”) as pbar:
for old_key in file_keys:
new_key = old_key.replace(source_dir, target_dir, 1)
try:
# 复制到新位置
s3.copy_object(
Bucket=bucket,
CopySource={'Bucket': bucket, 'Key': old_key},
Key=new_key
)
# 打印并更新进度条
print(f"Successfully copied: {old_key} → {new_key}")
pbar.update(1)
except Exception as e:
# 如果出现错误,显示错误信息
print(f"Error processing {old_key}: {str(e)}")
pbar.update(1) # 即使出错,继续进度条
这是我使用的代码,它理论上是正确的吗?