【发布时间】:2020-10-21 06:44:46
【问题描述】:
因此,我将首先说明我的老板要求我在没有 Google 存储桶实例或 Google Cloud Platform 的 .json 密钥文件的情况下设置 Google Cloud Storage 备份流程。基本上,我正在编写一个脚本来批量处理 Google Cloud Platform 机器的 Linux 和 Windows 实例,以获取将每天存储的备份文件,将其发送到 Google Cloud Storage,然后从系统(以便备份文件不占用实例空间)。由于所有这些对我来说都是相对较新的,我希望获得一些关于此过程是否需要密钥(.json 私钥)以及如何包含它的建议;显然,我没有密钥,但我想在我们设置好之前添加一个占位符。
我发现 Google 的文档很有帮助,但有点让人不知所措。这是我用来参考的链接:https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-code-sample
这是我的代码:
<?php
require_once 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
// Set up storage client with keyfilepath or .json key necessary?
// Set the parameters for Google cloud bucket name, filename, and path to file.
$bucketName = "fishbowl-storage";
$fileNameA = "backup_filename";
$fileNameB = "quickbook_filename";
$dataBackupSource = "";
$dataQBBackupSource = "";
// Set the function that will upload the file to google Cloud Storage.
function upload_object($bucketName, $fileName, $source)
{
// Create the storage client and store the file in the cloud.
$storage = new StorageClient();
$file = fopen($source, 'r');
$bucket = $storage->bucket($bucketName);
$file = $bucket->upload($file, [
'name' => $fileName
]);
// Print the success message to the user.
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $fileName);
}
// Check the OS to determine whether Linux or Windows.
if(php_uname('s') == "Linux"){
// Set the file path to grab back up.
$dataBackupSource = "../Fishbowl/backup";
$dataQBBackupSource = "../Fishbowl/otherbackup";
// Use try/catch statement to catch issues with private key.
try{
// run the function to place both backup files in the Google Cloud Storage Bucket instance.
upload_object($bucketName, $fileNameA, $dataBackupSource);
upload_object($bucketName, $fileNameB, $dataQBBackupSource);
// Delete the old file stored in the path.
if(!unlink($fileNameA)){
print ("The file cannot be deleteted or isn't present in this directory...");
}
else{
print ("The file " . $fileNameA . " has been deleted.");
}
}
catch (Exception $e){
print "This process failed for the following reason: " . $e;
return false;
}
}
else if(php_uname('s') == "Windows NT"){
// Set the file path to grab back up.
$dataBackupSource = "../Fishbowl/backup";
$dataQBBackupSource = "../Fishbowl/otherbackup";
// Use try/catch statement to catch issues with private key.
try{
// run the function to place both backup files in the Google Cloud Storage Bucket instance.
upload_object($bucketName, $fileNameA, $dataBackupSource);
upload_object($bucketName, $fileNameB, $dataQBBackupSource);
// Delete the old file stored in the path.
if(!unlink($fileNameA)){
print ("The file cannot be deleteted or isn't present in this directory...");
}
else{
print ("The file " . $fileNameA . " has been deleted.");
}
}
catch (Exception $e){
print "This process failed for the following reason: " . $e;
return false;
}
}
else{
print "The operating system has another name...";
}
?>
TLDR:我是否需要在我的脚本中包含 JSON 密钥才能将文件传递到 Google 云存储,如果需要,我应该把它放在哪里?
【问题讨论】:
-
经过一番研究并获得所需的 .JSON 文件的访问权限后,我找到了解决方案。本质上,我需要另外两个引用授权的部分。我将其放在此处,以防其他人遇到类似问题并需要解决方案。
-
使用此站点:zatackcoder.com/upload-file-to-google-cloud-storage-using-php 并使用您从 Google 下载的 .JSON 信息创建 privateKeyFileContent。然后,在下一条评论中设置与此类似的上传功能...
-
``` lang-php 函数 upload_object($bucketName, $fileName, $source) { $privateKeyFileContent = $GLOBALS['privateKeyFileContent']; // 创建存储客户端并将文件存储在云端。 $storage = new StorageClient([ 'keyFile' => json_decode($privateKeyFileContent, true) ]); $file = fopen($source, 'r'); $bucket = $storage->bucket($bucketName); $object = $bucket->upload($file, [ 'name' => $fileName ]); printf('上传 %s 到 gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $fileName); } ```
-
@Andie,感谢您的评论
标签: php json google-cloud-platform google-cloud-storage