簡體   English   中英

如何在 Google Cloud Function 中生成鏈接到特定存儲桶 GCS 的預簽名 URL?

[英]How do I generate a pre-signed URL in Google Cloud Function that links to a specific bucket GCS?

如何將使用服務帳號生成的私鑰文件添加到雲功能中進行身份驗證?

下面的代碼返回一個身份驗證憑據錯誤,它說我需要一個私鑰來簽署憑據。 如何創建它並傳入雲函數?

import datetime

from google.cloud import storage


def generate_download_signed_url_v4(bucket_name, blob_name):
    """Generates a v4 signed URL for downloading a blob.

    Note that this method requires a service account key file. You can not use
    this if you are using Application Default Credentials from Google Compute
    Engine or from the Google Cloud SDK.
    """
    # bucket_name = 'your-bucket-name'
    # blob_name = 'your-object-name'

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(blob_name)

    url = blob.generate_signed_url(
        version="v4",
        # This URL is valid for 15 minutes
        expiration=datetime.timedelta(minutes=15),
        # Allow GET requests using this URL.
        method="GET",
    )

    print("Generated GET signed URL:")
    print(url)
    print("You can use this URL with any user agent, for example:")
    print("curl '{}'".format(url))
    return url

我可以幫助您找到有關如何使用 Python 在 GCF 中創建簽名 url 的信息。 您需要使用Cloud Storage Python 客戶端庫,如官方 GCP 文檔中的此示例中所述 Medium 上的這個例子也可以幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM