因此,我首先要说明的是,我的老板要求我在没有Google Bucket实例或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 Cloud Storage吗?如果需要,我应该将它放在哪里?
发布于 2020-07-01 08:15:52
我想这就是你要找的documentation。基本上,您需要使用Google Cloud Storage的云客户端库API.These是您需要遵循的步骤
1.安装客户端库
composer需要google/云存储
2.设置身份验证。在此step中,您将创建服务帐户和密钥
设置环境变量后,在使用Google Cloud客户端库时,您不需要在代码中显式指定凭据,正如前面提到的here
3.使用客户端库。在本例中,您希望使用upload a file。
use Google\Cloud\Storage\StorageClient;
/**
* Upload a file.
*
* @param string $bucketName the name of your Google Cloud bucket.
* @param string $objectName the name of the object.
* @param string $source the path to the file to upload.
*
* @return Psr\Http\Message\StreamInterface
*/
function upload_object($bucketName, $objectName, $source)
{
$storage = new StorageClient();
$file = fopen($source, 'r');
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($file, [
'name' => $objectName
]);
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source),
$bucketName, $objectName);
}https://stackoverflow.com/questions/62666329
复制相似问题