版本控制
开启版本控制,读取旧版本,处理删除标记。
说明
桶版本控制在 MVP 范围内属于受支持功能;本页后半部分介绍的生命周期规则、对象锁定与桶硬配额属于预览功能。
本教程启用桶版本控制,写入同一对象的多个版本,按 VersionId 读取历史版本,并配置生命周期过期规则与桶硬配额。请沿用快速上手与使用 S3 SDK 中的 silo-admin profile 和 mcli_pg 别名。
保留与读取历史版本
创建独立的 version-demo 桶并开启版本控制,连续写入两个版本的 note.txt,再按第一个版本的 VersionId 读取原始正文:
aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api create-bucket --bucket version-demo
aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api put-bucket-versioning \
--bucket version-demo --versioning-configuration Status=Enabled
printf 'version one\n' > "$SILO_TUTORIAL_DIR/version-one.txt"
printf 'version two\n' > "$SILO_TUTORIAL_DIR/version-two.txt"
SILO_VERSION_1="$(aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api put-object \
--bucket version-demo --key note.txt --body "$SILO_TUTORIAL_DIR/version-one.txt" --query VersionId --output text)"
aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api put-object \
--bucket version-demo --key note.txt --body "$SILO_TUTORIAL_DIR/version-two.txt"
aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api list-object-versions --bucket version-demo
aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api get-object \
--bucket version-demo --key note.txt --version-id "$SILO_VERSION_1" "$SILO_TUTORIAL_DIR/version-one.download.txt"
cmp "$SILO_TUTORIAL_DIR/version-one.txt" "$SILO_TUTORIAL_DIR/version-one.download.txt"
在开启版本控制的桶中,不带 --version-id 的普通 delete-object 会插入一条删除标记(delete marker),原有历史版本仍会保留。如需彻底删除特定版本,必须显式传入 --version-id 并具备 s3:DeleteObjectVersion 权限;读取历史版本则需要 s3:GetObjectVersion 权限。
暂停版本控制(Status=Suspended)会使后续写入采用 null 版本语义,不会清空已有的历史版本。
生命周期规则与桶配额
创建 lifecycle-demo 桶,并设置一条生命周期规则:让 tmp/ 前缀下的当前对象在 7 天后过期,并在 1 天后终止未完成的分片上传:
aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api create-bucket --bucket lifecycle-demo
aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api put-bucket-lifecycle-configuration \
--bucket lifecycle-demo \
--lifecycle-configuration '{"Rules":[{"ID":"temporary-files","Status":"Enabled","Filter":{"Prefix":"tmp/"},"Expiration":{"Days":7},"AbortIncompleteMultipartUpload":{"DaysAfterInitiation":1}}]}'
可写网关上的后台工作协程会在数据库租约下定期扫描符合条件的对象。清理历史版本需配置 NoncurrentVersionExpiration 规则。远端存储分层规则(Transition / NoncurrentVersionTransition)不受支持,网关会直接拒绝。
桶硬配额统计桶内所有已提交当前版本与历史版本的对象正文及注解字节数(删除标记不计入配额)。未完成的分片上传虽然已经占用物理表空间,但在 CompleteMultipartUpload 发布对象之前不会计入逻辑桶配额:
mcli_pg quota set pg-admin/attachments --size 1GiB
mcli_pg quota info pg-admin/attachments
删除版本会立即释放逻辑配额,而物理表空间的回收与复用则由 PostgreSQL VACUUM 异步完成。详见存储布局。
扫码打开此页
https://mimus.pgsty.com/zh/docs/s3/tutorial/versioning/