Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

布灵图床后端允许使用环境变量设置R2和D1 #3

Open
aaro-n opened this issue Nov 19, 2024 · 1 comment
Open

布灵图床后端允许使用环境变量设置R2和D1 #3

aaro-n opened this issue Nov 19, 2024 · 1 comment

Comments

@aaro-n
Copy link

aaro-n commented Nov 19, 2024

R2的默认名称为:buling-imgbed-r2
D1的默认名称为:buling-imgbed-d1
我希望可以通过环境创建R2和D1,因为创建的buling-imgbed-r2和buling-imgbed-d1都在美国东海岸,要使用环境变量,我改了就无法和buling-imgbed同步代码了

这是gpt给的实例代码:

了解了!你提到的 buling-imgbed-r2 存储桶的默认值处理方式可以在工作流中进一步明确。以下是对相关步骤的改进建议,以确保在环境变量未设置时使用默认值:

建议的改进

  1. 明确环境变量的使用
    在创建 R2 存储桶的步骤中,明确说明如果环境变量未设置,将使用默认值 buling-imgbed-r2

  2. 示例代码
    可以在创建 R2 存储桶的步骤中加入逻辑,以便在环境变量未设置时使用默认值。示例如下:

      - name: 4.Create R2 Bucket if not exists
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
        run: |
          # 获取存储桶名称,如果环境变量未设置,则使用默认值
          BUCKET_NAME=${{ secrets.R2_BUCKET_NAME || 'buling-imgbed-r2' }}
          
          BUCKET_INFO=$(wrangler r2 bucket list | grep "$BUCKET_NAME" || echo "")
          if [ -z "$BUCKET_INFO" ]; then
            echo "Creating R2 bucket: $BUCKET_NAME..."
            wrangler r2 bucket create "$BUCKET_NAME" --location=apac
          else
            echo "R2 bucket '$BUCKET_NAME' already exists"
          fi

说明

  • 在上述代码中,使用 BUCKET_NAME 变量来决定存储桶的名称。如果环境变量 R2_BUCKET_NAME 未设置,则使用默认值 buling-imgbed-r2
  • 这样可以确保在没有提供环境变量时,工作流仍然能够顺利运行,同时提高了代码的可读性。

通过这种方式,你的工作流将更具灵活性和鲁棒性,能够更好地适应不同的部署环境。

@wzs8
Copy link
Owner

wzs8 commented Nov 20, 2024

已经把多存储桶支持(包括 加入B2存储桶 和 R2存储桶 切换)列入了未来的开发计划。

目前的话:

  • R2 上存储的图片,Cloudflare 会默认进行全球缓存,所以存储桶的地区不会影响使用。
  • 部署时可以指定存储桶的地域(默认是亚太地区)。
  • 已经部署的项目,如果需要切换存储桶,暂时需要手动修改配置。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants