那么真的可以在PHP中访问私有Google云存储桶吗?
我已经尝试过使用gsutil工具,但这并不能完全满足我的需求。
帮助将不胜感激。
答案 0 :(得分:2)
首先,您必须创建一个用于身份验证的应用程序帐户。转到Google Play Developer Console>>设置>>页面底部的API访问点击"创建帐户" (不是OAuth)。然后为已创建的帐户设置权限并在JSON中生成密钥文件。应该是这样的:
{
"type": "service_account",
"project_id": "api-...",
"private_key_id": "...",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
"client_email": "...@...iam.gserviceaccount.com",
"client_id": "...",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/..."
}
下一步使用PHP脚本中生成的凭据。在Composer中Google Cloud Client Library下载或要求。
使用Google Cloud Client Library方法授权和下载报告文件:
use Google\Cloud\Storage\StorageClient;
$client = new StorageClient([
'scopes' => [StorageClient::READ_ONLY_SCOPE],
'keyFile' => json_decode(file_get_contents('yourKeyFile.json'), true)
]);
$bucket = $client->bucket('pubsite_prod_rev_*'); //Find your bucket name in Google Developer Console >> Reports
//List all objects in bucket
foreach ($bucket->objects(['prefix' => 'stats/installs/']) as $object) {
print_r($object->name());
}
//Download some file
$file = $bucket->object('stats/installs/installs_*_overview.csv');
$file->downloadToFile();
//Or print as string
echo $file->downloadAsString();