【问题标题】:How to generate Signed URL for google cloud storage objects using PHP如何使用 PHP 为谷歌云存储对象生成签名 URL
【发布时间】:2013-12-27 21:18:36
【问题描述】:

我尝试使用的方法是使用 openssl

$fp = fopen($key, 'r');  //open the PEM file
$priv_key = fread($fp,8192);
fclose($fp);
$pkeyid = openssl_get_privatekey($priv_key,"password");

openssl_sign($response["data_to_sign"], $signature, $pkeyid,'sha256');

$sign = base64_encode($signature)

这是在谷歌中为签名网址生成签名的正确方法吗?

【问题讨论】:

    标签: php google-app-engine google-cloud-storage


    【解决方案1】:

    您可以试试 Google Cloud Storage PHP SDK,它是保持代码整洁的不错选择。

    云存储 PHP SDK

    按照此页面将软件包安装到您的项目 on Packagist, 那么

    function getSignedGcsUrl($objPath/* which is your target object path */, $duration = 50) 
    {
        $storageClient = new StorageClient([
            'projectId' => /* your gcp projectId here */,
            'keyFilePath' => /* your gcp keyFilePath here */,
        ]);
        $bucket = $storageClient->bucket($objPath);
        $object = $bucket->object();
        $url = $object->signedUrl(new \DateTime('+ ' . $duration . ' seconds'));
        return $url;
    }
    

    laravel-google-cloud-storage(用于 Laravel)

    按照此页面安装和配置 superbalist/laravel-google-cloud-storage: on Github, 那么

    public static function getSignedGcsUrl($objPath, $duration = 50)
    {
        return Storage::disk('gcs'/* following your filesystem configuration */)
            ->getAdapter()
            ->getBucket()
            ->object($objPath)
            ->signedUrl(new \DateTime('+ ' . $duration . ' seconds'));
    }
    

    【讨论】:

    • 2019年最准确的答案
    • 像 laravel 的魅力一样工作
    【解决方案2】:

    我把所有的答案放在一起。这应该在开箱即用的项目中工作。如果路径中有空格,则需要rawurlencode 各个组件,而不是urlencode

    function signedGoogleStorageURL($bucketName, $resourcePath, $duration = 10, $method = 'GET')
    {
        $expires = time() + $duration;
        $content_type = ($method == 'PUT') ? 'application/x-www-form-
        urlencoded' : '';
        $to_sign = ($method . "\n" .
                    /* Content-MD5 */ "\n" .
                    $content_type . "\n" .
                    $expires . "\n" .
                    "/" . $bucketName . $resourcePath);
    
        $sign_result = AppIdentityService::signForApp($to_sign);
        $signature = urlencode(base64_encode($sign_result['signature']));
        $email = AppIdentityService::getServiceAccountName();
    
        return ('https://storage.googleapis.com/' . $bucketName .
                $resourcePath .
                '?GoogleAccessId=' . $email .
                '&Expires=' . $expires .
                '&Signature=' . $signature);
    }
    
    $signedPath = signedGoogleStorageURL(AppIdentityService::getDefaultVersionHostname(), "/my_folder/my_file", 60);
    

    【讨论】:

      【解决方案3】:

      需要注意的是,我花了大约两个小时:

      您传递到 URL 的 GoogleAccessId 是 Google Cloud Console 的“证书”部分中的电子邮件地址。这不是 Google 在其文档中建议的带有字符串替换的 OAuth 客户端 ID。

      【讨论】:

        【解决方案4】:

        这里有一个示例,它使用 PHP 为 Google Cloud Storage 的 URL 签名: https://groups.google.com/forum/#!msg/google-api-php-client/jaRYDWdpteQ/xbNTLfDhUggJ

        但是 - 我注意到这是用 Google App Engine 标记的...如果您的代码在 Google App Engine 内运行,您应该使用内置的 App Identity 服务 - (请注意,这仅在您的应用程序部署后才有效在生产中,而不是在本地运行时)-这意味着您不需要下载或处理任何私钥:

        require_once 'google/appengine/api/app_identity/AppIdentityService.php';
        
        $sign_result = AppIdentityService::signForApp( $message );
        

        您需要确保将与 App Engine 应用程序关联的服务帐号添加到拥有 Cloud Storage 存储分区的项目的团队中。

        【讨论】:

        • 不使用google app engine时的方法是什么?
        • 使用已下载的私钥创建服务帐户(请参阅developers.google.com/accounts/docs/OAuth2ServiceAccount)。该私钥可用于以相同方式对数据进行签名。上面 Google Group 论坛的链接显示了如何使用 openssl_sign 来完成此操作。 php.net/manual/en/function.openssl-sign.php 展示了如何读取私钥文件的示例。
        • 在它说的部分,“*签名将在这里*”是什么意思?我指的是您使用 openssl_sign 给出的示例。 (关于这个例子,我最困惑的是你应该插入什么信息)
        • 是的——这令人困惑。查看 openssl_sign 的文档,它在成功时会覆盖该值——即 PHP 通过引用而不是值传递该字符串。这有意义吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-15
        • 2014-10-23
        • 2019-03-16
        • 2018-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多