如何包括谷歌应用引擎PHP在我的脚本/自动加载


How to include Google App Engine for PHP in my scripts / autoloading?

我在Ubuntu网络服务器上有一个网站(不是应用程序,也不是在app Engine上托管),我想使用谷歌云存储上传/下载大文件。我正试图将文件直接上传到谷歌云存储不工作(也许是因为我犯了一些基本错误)。

我已经安装了Google Cloud SDK,下载并解压缩了Google App Engine。如果我现在包含CloudStorageTools.php,我得到一个错误:

类"google'appengine'CreateUploadURLRequest"未找到

我的脚本是这样的:

require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google'appengine'api'cloud_storage'CloudStorageTools;
$options = [ 'gs_bucket_name' => 'test' ];
$upload_url = CloudStorageTools::createUploadUrl( '/test.php' , $options );

如果你想使用Google App Engine (gae)的功能,你将需要在gae上托管,这可能会对你的应用程序架构产生更大的影响(它使用自定义的Google编译php版本,库有限,没有本地文件处理,所以所有的功能需要进入blob store或gcs - Google Cloud Storage)。

对于运行在ubuntu上的PHP应用程序,您最好使用google-api-php-client来连接到存储JSON api。不幸的是,该文档对php来说不是很好。您可以在如何重命名或移动谷歌云存储(PHP API)中的文件检查我的答案,看看如何获取/复制/删除对象。要上传,我建议检索预先签名的上传URL,如下所示:

//get google client and auth token for request
$gc = 'Google::getClient();
if($gc->isAccessTokenExpired())
    $gc->getAuth()->refreshTokenWithAssertion();
$googleAccessToken = json_decode($gc->getAccessToken(), true)['access_token'];
//compose url and headers for upload url request
$initUploadURL = "https://www.googleapis.com/upload/storage/v1/b/"
    .$bucket."/o?uploadType=resumable&name="
    .urlencode($file_dest);
//Compose headers
$initUploadHeaders = [
    "Authorization"             =>"Bearer ".$googleAccessToken,
    "X-Upload-Content-Type"     => $mimetype,
    "X-Upload-Content-Length"   => $filesize,
    "Content-Length"            => 0,
    "Origin"                    => env('APP_ADDRESS')
];
//send request to retrieve upload url
$req = $gc->getIo()->makeRequest(new 'Google_Http_Request($initUploadURL, 'POST', $initUploadHeaders));
// pre signed upload url that allows client side upload
$presigned_upload_URL = $req->getResponseHeader('location');

将该URL发送到您的客户端后,您可以使用它将文件直接放到bucket中,并使用生成适当PUT请求的上传脚本。下面是AngularJS中使用ng-file-upload:

的例子
file.upload = Upload.http({
    url: uploadurl.url,
    skipAuthorization: true,
    method: 'PUT',
    filename: file.name,
    headers: {
        "Content-Type": file.type !== '' ? file.type : 'application/octet-stream'
    },
    data: file
});

祝你好运- gcs是一个艰难的,如果你不想去谷歌所有的应用引擎!

Google API PHP客户端允许您连接到任何Google API,包括云存储API。这里有一个例子,这里有一个入门指南。