【问题标题】:How to transfer single file to Google Cloud Storage Through Script如何通过脚本将单个文件传输到谷歌云存储
【发布时间】: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


【解决方案1】:

我认为这就是您正在寻找的documentation。基本上,您需要为 Google Cloud Storage API 使用 Cloud Client Libraries。这些是您需要遵循的步骤

1.安装客户端库

作曲家需要 google/cloud-storage

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);
}

【讨论】:

  • 感谢您的跟进。我已经在需要库的两个系统上安装了 composer,并设置为提供 cloud/storage/storageclient 库。但是,我对 .json 键以及是否需要包含它更加好奇。您知道在我的脚本中是否需要调用密钥? //导出 GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json";
  • 否 :),设置环境变量后,在使用 Google Cloud 客户端库时,您无需在代码中明确指定您的凭据,如提及 here
  • 所以我需要这行代码吗?导出 GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"
  • GOOGLE_APPLICATION_CREDENTIALS 是环境变量,正如我在上一条评论中提到的那样,不,您不需要在代码中添加该行。
  • 哦,这样会放在我的 settings.json 文件中吗?
猜你喜欢
  • 2018-04-02
  • 2017-04-08
  • 2017-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多