在线客服

Google Cloud Cloud CDN如何配置签名URL,限制对私有内容的访问

⏱️2026-03-29 09:00 👁️3

配置 Google Cloud CDN 签名 URL 可以有效限制对私有内容的访问。以下是详细步骤,包含必要的代码示例和解释,希望能帮你更好地理解和操作!🚀

  1. 创建 Cloud Storage Bucket (如果还没有) 📁

    首先,你需要一个 Cloud Storage Bucket 来存放你的私有内容。 确保 Bucket 设置为私有(即,没有公共访问权限)。

    gsutil mb -l <location> -p <project_id> gs://<your-bucket-name>
    gsutil defacl set private gs://<your-bucket-name>
    gsutil iam ch allUsers: gs://<your-bucket-name>
    • <location>: Bucket 的地理位置 (例如, US, asia-east1)。
    • <project_id>: 你的 Google Cloud 项目 ID。
    • <your-bucket-name>: 你要创建的 Bucket 的名称。
  2. 创建 Cloud CDN 密钥 🔑

    Cloud CDN 使用密钥来验证签名 URL。 你需要在 Cloud CDN 上创建一个密钥,并安全地保存它。

    1. 使用 gcloud CLI 创建密钥

      gcloud compute backend-buckets create <backend-bucket-name> \
          --gcs-bucket-name=<your-bucket-name> \
          --enable-cdn
      
      gcloud compute backend-buckets update <backend-bucket-name> --enable-cdn
      
      gcloud compute backend-buckets add-signed-url-key --backend-bucket=<backend-bucket-name> --key-name=<key-name>
      
      • <backend-bucket-name>: 后端 Bucket 的名称 (例如, my-cdn-bucket)。
      • <your-bucket-name>: 你的 Cloud Storage Bucket 名称。
      • <key-name>: 密钥的名称 (例如, my-cdn-key)。

      密钥将自动生成。 密钥值需要保存好,因为你将用它来生成签名 URL。

    2. 或者,使用 Cloud Console 创建密钥

      1. 导航到 Google Cloud Console 中的 “Cloud CDN”。
      2. 选择你的后端 Bucket。
      3. 在 "Signed URLs" 部分,点击 "Add Key"。
      4. 输入密钥名称,然后点击 "Create"。
      5. 安全地保存生成的密钥值。
  3. 生成签名 URL ✍️

    使用你创建的密钥和要访问的资源路径来生成签名 URL。 你需要使用一段代码来完成这个任务。以下是一个 Python 示例:

    import base64
    import hashlib
    import hmac
    import time
    import urllib.parse
    
    def generate_signed_url(base_url, key_name, key_value, expiration_time):
        """Generates a signed URL for Cloud CDN.
    
        Args:
            base_url: The base URL of the resource (e.g., "https://example.com/my-image.jpg").
            key_name: The name of the Cloud CDN key.
            key_value: The secret value of the Cloud CDN key (bytes).
            expiration_time: The expiration timestamp in seconds since the epoch.
    
        Returns:
            A signed URL.
        """
        url_to_sign = base_url + '?Expires=' + str(expiration_time) + '&KeyName=' + key_name
        url_to_sign_bytes = url_to_sign.encode('utf-8')
    
        signature = base64.urlsafe_b64encode(hmac.new(key_value, url_to_sign_bytes, hashlib.sha1).digest()).decode('utf-8')
        signed_url = url_to_sign + '&Signature=' + signature
    
        return signed_url
    
    # Example Usage
    base_url = "https://your-domain.com/your-file.txt" # 替换成你的CDN域名和文件路径
    key_name = "my-cdn-key" # 替换成你的key name
    key_value = b"YOUR_KEY_VALUE" # 替换成你的key value (byte类型)
    expiration_time = int(time.time()) + 3600  # URL 在 1 小时后过期
    
    signed_url = generate_signed_url(base_url, key_name, key_value, expiration_time)
    print(f"Signed URL: {signed_url}")
    
    • base_url: 你的 CDN 域名加上你要访问的文件的路径。例如: https://cdn.example.com/images/my-image.jpg务必使用你的CDN域名
    • key_name: 你在 Cloud CDN 上创建的密钥的名称。
    • key_value: 你创建的密钥的实际值 (字节类型)。 务必保存好密钥值,不要泄露
    • expiration_time: URL 的过期时间 (Unix 时间戳)。

    重要提示:确保 key_value 以字节形式传递(例如,b"your-secret-key")。

  4. 配置 CDN 域名 🌐 (如果还没有)

    你需要将你的域名指向 Cloud CDN。 这通常涉及到配置 DNS 记录。 具体步骤取决于你的域名注册商。

  5. 测试签名 URL 🧪

    使用生成的签名 URL 在浏览器或使用 curl 命令访问你的私有内容。 确保 URL 在过期时间之前有效。

    curl "<your-signed-url>"
  6. 错误处理 ❗

    如果签名 URL 无效或已过期,Cloud CDN 将返回 403 Forbidden 错误。 确保你的代码能正确处理这些错误情况。

  7. 安全性建议 🛡️

    • 轮换密钥:定期更换你的 Cloud CDN 密钥,以提高安全性。
    • 安全存储密钥:不要将密钥存储在代码中。 使用环境变量、密钥管理服务(如 Google Cloud Secret Manager)或其他安全的方法来存储密钥。
    • 限制过期时间:尽可能使用较短的过期时间,以减少 URL 被滥用的风险。

希望这个详细的配置指南能帮助你成功配置 Cloud CDN 签名 URL! Good luck! 🎉

鲨鱼云自助平台

鲨鱼云自助平台是一站式国际云服务解决方案平台,支持阿里云国际、腾讯云国际、亚马逊AWS、谷歌云GCP等主流云厂商账号的开通、充值与管理。

热门文章
更多>