【问题标题】:Cloud storage and secure download strategy on app engine. GCS acl or blobstore应用引擎上的云存储和安全下载策略。 GCS acl 或 blobstore
【发布时间】:2015-07-03 01:46:19
【问题描述】:

我的 appengine 应用会创建云存储文件。这些文件将由第三方下载。这些文件包含个人医疗信息

首选的下载方式是什么:

  1. 使用带有用户 READER acl 的直接 GCS 下载链接。
  2. 或者在 appengine 应用中使用 blobstore 下载处理程序。

这两种解决方案都需要第三方登录(谷歌登录)。性能不是问题。隐私和安全错误和错误的发生都是。

使用加密的 zip 文件进行下载是一种选择。这意味着我必须将密码存储在项目中。或者通过电子邮件发送一个随机密码?

更新我用来创建签名下载网址的 appengine 代码

import time
import urllib
from datetime import datetime, timedelta
from google.appengine.api import app_identity
import os
import base64

API_ACCESS_ENDPOINT = 'https://storage.googleapis.com'

# Use the default bucket in the cloud and not the local SDK one from app_identity
default_bucket = '%s.appspot.com' % os.environ['APPLICATION_ID'].split('~', 1)[1]
google_access_id = app_identity.get_service_account_name()


def sign_url(bucket_object, expires_after_seconds=60):
    """ cloudstorage signed url to download cloudstorage object without login
        Docs : https://cloud.google.com/storage/docs/access-control?hl=bg#Signed-URLs
        API : https://cloud.google.com/storage/docs/reference-methods?hl=bg#getobject
    """

    method = 'GET'
    gcs_filename = '/%s/%s' % (default_bucket, bucket_object)
    content_md5, content_type = None, None

    expiration = datetime.utcnow() + timedelta(seconds=expires_after_seconds)
    expiration = int(time.mktime(expiration.timetuple()))

    # Generate the string to sign.
    signature_string = '\n'.join([
        method,
        content_md5 or '',
        content_type or '',
        str(expiration),
        gcs_filename])

    _, signature_bytes = app_identity.sign_blob(signature_string)
    signature = base64.b64encode(signature_bytes)

    # Set the right query parameters.
    query_params = {'GoogleAccessId': google_access_id,
                    'Expires': str(expiration),
                    'Signature': signature}

    # Return the download URL.
    return '{endpoint}{resource}?{querystring}'.format(endpoint=API_ACCESS_ENDPOINT,
                                                       resource=gcs_filename,
                                                       querystring=urllib.urlencode(query_params))

【问题讨论】:

    标签: google-app-engine authentication google-cloud-storage acl blobstorage


    【解决方案1】:

    如果少数用户可以访问存储桶中的所有文件,那么解决方案 #1 就足够了,因为管理 ACL 不会太痛苦。

    但是,如果您有许多不同的用户,每个用户都需要对存储桶中的不同文件进行不同的访问,那么解决方案 #1 是不切实际的。

    我也会避免使用解决方案 #2,因为您需要为不必要的传入/传出 GAE 带宽付费。

    也许要考虑的第三种解决方案是使用 App Engine 处理身份验证,并编写逻辑来确定哪些用户可以访问哪些文件。然后,当请求下载文件时,您创建Signed URLs 以直接从 GCS 下载数据。您可以将过期参数设置为适合您的值,这会使 URL 在设定的时间后失效。

    【讨论】:

    • 感谢第三个解决方案。您的答案的第一部分已经到位。带有过期的签名 url 是个好主意。没有 acl 管理和直接链接下载。但是知道 URL 的任何人都可以在有限的时间内访问该资源。
    • 网址不明确,您可以将到期时间设置为几秒钟。有足够的时间重定向到您的用户并开始下载。
    • 是的。这使得几乎不可能劫持链接。
    • 我已经用 Python 代码更新了我的答案。在 appengine 中非常简单,使用 app_endenty 对签名字符串进行签名。
    猜你喜欢
    • 2018-05-09
    • 2011-03-06
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    相关资源
    最近更新 更多