简体   繁体   English

如何下载谷歌CDN图片?

[英]How to download google CDN images?

In google, images are hosted in CDN URL type and I tried to download as an image from that CDN but it throws an error in C#. Used this c# code attached below.在谷歌中,图像托管在 CDN URL 类型中,我尝试从该 CDN 下载图像,但它在 C# 中抛出错误。使用下面附上的 c# 代码。

          using (var webClient = new WebClient())
            {
                byte[] imageBytes = webClient.DownloadData(imageUUl);
                System.IO.File.WriteAllBytes(@"E:\Temp\img2.jpeg", imageBytes);
            }

URL: https://lh6.googleusercontent.com/vpsleVfq12ZnALrwbIUqCTa0Fpqa5C8IUViGkESOSqvHshQpKCyOq4wsRfTcadG2WYgcW3m0yq_6M2l_IrSM3qr35spIML9iyIHEULwRu4mWw4CUjCwpVfiWnd5MXPImMw=w1280 URL: https://lh6.googleusercontent.com/vpsleVfq12ZnALrwbIUqCTa0Fpqa5C8IUViGkESOSqvHshQpKCyOq4wsRfTcadG2WYgcW3m0yq_6M2l_IrSM3qr35spIML9iyIHEULwRu4mWw4CUjCwpVfiWnd5MXPImMw=w1280

Thanks in advance.提前致谢。

In GCP Cloud CDN, you can use a signed URL with authentication or signed cookies to authorize users and provide them with a time-limited token for accessing your protected content, so a Cloud CDN does not block requests without a signature query parameter or Cloud-CDN-Cookie HTTP cookie.在 GCP 云 CDN 中,您可以使用带身份验证的签名 URL 或签名 cookies 来授权用户并为他们提供访问受保护内容的限时令牌,因此云 CDN 不会阻止没有签名查询参数或云- CDN-Cookie HTTP cookie。 It rejects requests with invalid (or otherwise malformed) request parameters, due to this I suggest reviewing your browser client security settings;它拒绝请求参数无效(或格式错误)的请求,因此我建议检查您的浏览器客户端安全设置; how the authentication is managed to your CDN URL. Some clients store cookies by default if the security policy is allowed, also review how your CDN URL security ingress is configured because when you are using a CDN URLs with signed cookies the responses to signed and unsigned requests are cached separately, so a successful response to a valid signed request is never used to serve an unsigned request.如何将身份验证管理到您的 CDN URL。如果允许安全策略,一些客户端默认存储 cookies,还要查看您的 CDN URL 安全入口是如何配置的,因为当您使用带有签名 cookies 的 CDN URL 时,对签名和未签名的响应请求被单独缓存,因此对有效签名请求的成功响应永远不会用于服务未签名的请求。 In another hand if you are using a signed CDN URL to limited secure access to file for a limited amount of time, there are some steps that you need to follow first:另一方面,如果您使用已签名的 CDN URL 在有限的时间内限制对文件的安全访问,则需要首先执行一些步骤:

  • Ensure that Cloud CDN is enabled.确保云 CDN 已启用。

  • If necessary, update to the latest version of the Google Cloud CLI:如有必要,请更新到最新版本的 Google Cloud CLI:

     `gcloud components update`
  • Creating keys for your signed URLs为您的签名网址创建密钥

    To create keys, follow these steps.要创建密钥,请按照下列步骤操作。

     1.In the Google Cloud Console, go to the Cloud CDN page. 2.Click Add origin. 3.Select an HTTP(S) load balancer as the origin. 4.Select backend services or backend buckets. For each one -Click Configure, and then click Add signing key. -Under Name, give the new signing key a name. -Under the Key creation method, select Automatically generate or Let me enter. - If you're entering your own key, type the key into the text field. - Click Done. - Under Cache entry maximum age, provide a value, and select a Unit of time from the drop-down list. You can choose among second, minute, hour, and day. The maximum amount of time is three (3) days. 5. Click Save. 6. Click Add.
  • Configuring Cloud Storage permissions.配置云存储权限。

Before you run the following command, add at least one key to a backend bucket in your project;在运行以下命令之前,请将至少一个密钥添加到项目中的后端存储桶; otherwise, the command fails with an error because the Cloud CDN cache fill service account is not created until you add one or more keys for the project.否则,该命令会失败并出现错误,因为在您为项目添加一个或多个密钥之前不会创建 Cloud CDN 缓存填充服务帐户。 Replace PROJECT_NUM with your project number and BUCKET with your storage bucket.将 PROJECT_NUM 替换为您的项目编号,将 BUCKET 替换为您的存储桶。

gsutil iam ch \ serviceAccount:service-PROJECT_NUM@cloud-cdn-fill.iam.gserviceaccount.com:objectViewer \ gs://BUCKET

  • List the keys on a backend service or backend bucket, run one of the following commands:列出后端服务或后端存储桶上的密钥,运行以下命令之一:

    gcloud compute backend-services describe BACKEND_NAME

    gcloud compute backend-buckets describe BACKEND_NAME

  • Sign URLs and distribute them.签署 URL 并分发它们。

You can sign URLs by using the gcloud compute sign-url command or by using code that you write yourself.您可以使用gcloud compute sign-url命令或使用您自己编写的代码对 URL 进行签名。 If you need many signed URLs, custom code provides better performance.如果您需要许多签名 URL,自定义代码可提供更好的性能。 This command reads and decodes the base64url encoded key value from KEY_FILE_NAME , and then outputs a signed URL that you can use for GET or HEAD requests for the given URL.此命令从KEY_FILE_NAME读取并解码 base64url 编码的键值,然后输出一个带符号的 URL,您可以将其用于给定 URL 的 GET 或 HEAD 请求。

gcloud compute sign-url \
  "https://example.com/media/video.mp4" \
  --key-name my-test-key \
  --expires-in 30m \
  --key-file sign-url-key-file

In this link , you can find more info related to signed URLs and signed cookies.在此链接中,您可以找到与签名 URL 和签名 cookies 相关的更多信息。

You can't download an image in that way, since you need to provide an OAuth token.您不能以这种方式下载图像,因为您需要提供OAuth 令牌。 and you need to have the profile scope enabled并且您需要启用配置文件scope

    var GoogleAuth; // Google Auth object.
function initClient() {
  gapi.client.init({
      'apiKey': 'YOUR_API_KEY',
      'clientId': 'YOUR_CLIENT_ID',
      'scope': 'https://www.googleapis.com/auth/userinfo.profile',
      'discoveryDocs': ['https://discovery.googleapis.com/discovery/v1/apis']
  }).then(function () {
      GoogleAuth = gapi.auth2.getAuthInstance();

      // Listen for sign-in state changes.
      GoogleAuth.isSignedIn.listen(updateSigninStatus);
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM