Skip to main content

Bucket locks

还没翻译译自英文(developers.cloudflare.com)本页 39 段都还没译

模型写的

背景方法设置结果分析局限

Bucket locks prevent the deletion and overwriting of objects in an R2 bucket for a specified period — or indefinitely. When enabled, bucket locks enforce retention policies on your objects, helping protect them from accidental or premature deletions.Bucket locks prevent the deletion and overwriting of objects in an R2 bucket for a specified period — or indefinitely. When enabled, bucket locks enforce retention policies on your objects, helping protect them from accidental or premature deletions.存储桶锁定在指定期限或永久阻止对象删除和覆盖 强制保留策略保护对象免遭意外或过早删除存储桶锁定在指定期限或永久阻止对象删除和覆盖存储桶锁定

Get started with bucket locks

你需要现有存储桶,使用 API 时还需 API 令牌

Before getting started, you will need:Before getting started, you will need:开始前需要先满足前提条件开始前需要先满足前提条件前提条件

  • An existing R2 bucket. If you do not already have an existing R2 bucket, refer to Create buckets.
    An existing R2 bucket.
    需要一个现有的 R2 存储桶 若尚未创建,请参阅创建存储桶
    需要一个现有的 R2 存储桶
    R2 存储桶
  • (API only) An API token with permissions to edit R2 bucket configuration.
    (API only) An API token with permissions to edit R2 bucket configuration.
    API 用户需要具备编辑 R2 存储桶配置权限的令牌
    API 用户需要具备编辑 R2 存储桶配置权限的令牌
    API 令牌

Enable bucket lock via dashboard

在仪表板设置选项卡添加锁定规则并保存

  1. In the Cloudflare dashboard, go to the R2 object storage page.

    dash.cloudflare.com

  2. Select the bucket you would like to add bucket lock rule to.

  3. Switch to the Settings tab, then scroll down to the Bucket lock rules card.

  4. Select Add rule and enter the rule name, prefix, and retention period.

  5. Select Save changes.

Enable bucket lock via Wrangler

使用 r2 bucket lock add 或通过 JSON 配置文件设置

  1. Install npm.
  2. Install Wrangler, the Developer Platform CLI.
  3. Log in to Wrangler with the wrangler login command.
  4. Add a bucket lock rule to your bucket by running the r2 bucket lock add command.
    Add a bucket lock rule to your bucket by running the r2 bucket lock add command.
    使用 r2 bucket lock add 添加锁定规则
    使用 r2 bucket lock add 添加锁定规则
    lock add
npx wrangler r2 bucket lock add <BUCKET_NAME> [OPTIONS]

Alternatively, you can set the entire bucket lock configuration for a bucket from a JSON file using the r2 bucket lock set command.Alternatively, you can set the entire bucket lock configuration for a bucket from a JSON file using the r2 bucket lock set command.使用 r2 bucket lock set 从 JSON 文件设置整个锁定配置使用 r2 bucket lock set 从 JSON 文件设置整个锁定配置lock set

npx wrangler r2 bucket lock set <BUCKET_NAME> --file <FILE_PATH>

The JSON file should be in the format of the request body of the put bucket lock configuration API.The JSON file should be in the format of the request body of the put bucket lock configuration API.JSON 文件须与 put bucket lock 配置请求体格式一致JSON 文件须与 put bucket lock 配置请求体格式一致JSON 格式

Enable bucket lock via API

向存储桶锁定接口 PUT 一个 rules 数组

For information about getting started with the Cloudflare API, refer to Make API calls. For information on required parameters and more examples of how to set bucket lock configuration, refer to the API documentation.For information about getting started with the Cloudflare API, refer to Make API calls. For information on required parameters and more examples of how to set bucket lock configuration, refer to the API documentation.API 入门和参数见链接文档API 入门和参数见链接文档API 文档

Below is an example of setting a bucket lock configuration (a collection of rules):Below is an example of setting a bucket lock configuration (a collection of rules):锁定配置是通过 PUT 设置的规则集合锁定配置是通过 PUT 设置的规则集合配置

curl -X PUT "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/r2/buckets/<BUCKET_NAME>/lock" \
    -H "Authorization: Bearer <API_TOKEN>" \
    -H "Content-Type: application/json" \
    -d '{
        "rules": [
            {
                "id": "lock-logs-7d",
                "enabled": true,
                "prefix": "logs/",
                "condition": {
                    "type": "Age",
                    "maxAgeSeconds": 604800
                }
            },
            {
                "id": "lock-images-indefinite",
                "enabled": true,
                "prefix": "images/",
                "condition": {
                    "type": "Indefinite"
                }
            }
        ]
    }'

This request creates two rules:This request creates two rules:该请求创建两条规则该请求创建两条规则两条规则

  • lock-logs-7d: Objects under the logs/ prefix are retained for 7 days (604800 seconds).
    lock-logs-7d: Objects under the logs/ prefix are retained for 7 days (604800 seconds).
    lock-logs-7d 将 logs/ 对象保留 7 天 604800 秒等于七天
    lock-logs-7d 将 logs/ 对象保留 7 天
    lock-logs-7d
  • lock-images-indefinite: Objects under the images/ prefix are locked indefinitely.
    lock-images-indefinite: Objects under the images/ prefix are locked indefinitely.
    lock-images-indefinite 永久锁定 images/ 对象
    lock-images-indefinite 永久锁定 images/ 对象
    lock-images-indefinite

Note

If your bucket is setup with jurisdictional restrictions, you will need to pass a cf-r2-jurisdiction request header with that jurisdiction. For example, cf-r2-jurisdiction: eu.If your bucket is setup with jurisdictional restrictions, you will need to pass a cf-r2-jurisdiction request header with that jurisdiction. For example, cf-r2-jurisdiction: eu.有辖区限制的存储桶需要 cf-r2-jurisdiction 请求头 例如,欧盟辖区需传入 cf-r2-jurisdiction: eu有辖区限制的存储桶需要 cf-r2-jurisdiction 请求头cf-r2-jurisdiction

Get bucket lock rules for your R2 bucket

Dashboard

在设置选项卡的存储桶锁定规则卡片查看规则

  1. In the Cloudflare dashboard, go to the R2 object storage page.

    dash.cloudflare.com

  2. Select the bucket you would like to add bucket lock rule to.

  3. Switch to the Settings tab, then scroll down to the Bucket lock rules card.

Wrangler

使用 r2 bucket lock list 列出规则

To list bucket lock rules, run the r2 bucket lock list command:To list bucket lock rules, run the r2 bucket lock list command:使用 r2 bucket lock list 列出锁定规则使用 r2 bucket lock list 列出锁定规则lock list

npx wrangler r2 bucket lock list <BUCKET_NAME>

API

通过 API 文档端点获取规则

For more information on required parameters and examples of how to get bucket lock rules, refer to the API documentation.For more information on required parameters and examples of how to get bucket lock rules, refer to the API documentation.通过 API 获取锁定规则的方法见 API 参考通过 API 获取锁定规则的方法见 API 参考API 参考

Remove bucket lock rules from your R2 bucket

Dashboard

在设置选项卡通过 ... 图标删除规则

  1. In the Cloudflare dashboard, go to the R2 object storage page.

    dash.cloudflare.com

  2. Select the bucket you would like to add bucket lock rule to.

  3. Switch to the Settings tab, then scroll down to the Bucket lock rules card.

  4. Locate the rule you want to remove, select the ... icon next to it, and then select Delete.

    Locate the rule you want to remove, select the ...
    通过规则旁的 ... 图标删除规则
    通过规则旁的 ... 图标删除规则
    ... 图标

Wrangler

使用 r2 bucket lock remove 和规则 id 删除规则

To remove a bucket lock rule, run the r2 bucket lock remove command:To remove a bucket lock rule, run the r2 bucket lock remove command:使用 r2 bucket lock remove 及规则 id 删除规则使用 r2 bucket lock remove 及规则 id 删除规则lock remove

npx wrangler r2 bucket lock remove <BUCKET_NAME> --id <RULE_ID>

API

从更新配置中排除规则即可删除

To remove bucket lock rules via API, exclude them from your updated configuration and use the put bucket lock configuration API.To remove bucket lock rules via API, exclude them from your updated configuration and use the put bucket lock configuration API.通过 API 删除规则需将其从更新配置中排除 put bucket lock 配置 API 会以新配置为准替换规则通过 API 删除规则需将其从更新配置中排除更新配置

Bucket lock rules

最多 1,000 条规则,最严格的保留要求优先

A bucket lock configuration can include up to 1,000 rules. Each rule specifies which objects it covers (via prefix) and how long those objects must remain locked. You can:A bucket lock configuration can include up to 1,000 rules. Each rule specifies which objects it covers (via prefix) and how long those objects must remain locked.配置最多包含 1,000 条规则,每条指定前缀和时长配置最多包含 1,000 条规则,每条指定前缀和时长1,000 条规则

  • Lock objects for a specific duration. For example, 90 days.
    Lock objects for a specific duration.
    规则可按特定时长锁定对象 例如 90 天
    规则可按特定时长锁定对象
    特定时长
  • Retain objects until a certain date. For example, until January 1, 2026.
    Retain objects until a certain date.
    规则可将对象保留至某个日期 例如直到 2026 年 1 月 1 日
    规则可将对象保留至某个日期
    某个日期
  • Keep objects locked indefinitely.
    Keep objects locked indefinitely.
    规则可永久锁定对象
    规则可永久锁定对象
    永久锁定

If multiple rules apply to the same prefix or object key, the strictest (longest) retention requirement takes precedence.If multiple rules apply to the same prefix or object key, the strictest (longest) retention requirement takes precedence.多规则重叠时最严格(最长)的保留要求优先 这解决了多条规则应用于同前缀或键时的冲突多规则重叠时最严格(最长)的保留要求优先最严格保留

Notes

锁定优先于生命周期规则,并阻止清空存储桶

  • Rules without prefix apply to all objects in the bucket.
    Rules without prefix apply to all objects in the bucket.
    无前缀规则应用于存储桶中所有对象
    无前缀规则应用于存储桶中所有对象
    所有对象
  • Rules apply to both new and existing objects in the bucket.
    Rules apply to both new and existing objects in the bucket.
    规则同时应用于新对象和现有对象
    规则同时应用于新对象和现有对象
    现有对象
  • Bucket lock rules take precedence over lifecycle rules. For example, if a lifecycle rule attempts to delete an object at 30 days but a bucket lock rule requires it be retained for 90 days, the object will not be deleted until the 90-day requirement is met.
    Bucket lock rules take precedence over lifecycle rules. For example, if a lifecycle rule attempts to delete an object at 30 days but a bucket lock rule requires it be retained for 90 days, the object will not be deleted until the 90-day requirement is met.
    存储桶锁定规则优先于生命周期规则 30 天到期的对象会保留至满足 90 天锁定要求
    存储桶锁定规则优先于生命周期规则
    生命周期规则
  • A bucket cannot be emptied while any bucket lock rules are configured. Remove all lock rules before emptying a bucket. Bucket lock rules also apply when deleting folders from the dashboard.
    A bucket cannot be emptied while any bucket lock rules are configured. Remove all lock rules before emptying a bucket.
    存在锁定规则时无法清空存储桶 从仪表板删除文件夹时规则同样适用
    存在锁定规则时无法清空存储桶
    清空存储桶