Php 在谷歌云存储中简单更新文本内容

Php 在谷歌云存储中简单更新文本内容,php,google-cloud-storage,Php,Google Cloud Storage,我是谷歌云存储的新用户,所以请容忍我 我正在尝试创建一个文件编辑器,它从GCS获取一个非二进制文件,并将其保存回去 我使用的是谷歌api php客户端。我已经用API做了很多实验,浏览过,但是我就是找不到正确的答案 <?php class GCS_Driver { /** @var Google_Service_Storage $driver */ public $driver; /** @var string $bucket */ public $buc

我是谷歌云存储的新用户,所以请容忍我

我正在尝试创建一个文件编辑器,它从GCS获取一个非二进制文件,并将其保存回去

我使用的是谷歌api php客户端。我已经用API做了很多实验,浏览过,但是我就是找不到正确的答案

<?php

class GCS_Driver {
    /** @var Google_Service_Storage $driver */
    public $driver;
    /** @var string $bucket */
    public $bucket;

    public function updateObject($objectPath,$content) {
        $updated = false;
        try {
            $postBody = new Google_Service_Storage_StorageObject();
            $updated = $this->driver->objects->patch($this->bucket,$objectPath,$postBody,array(
                'data' => $content // I know this is wrong, I am just showing the idea I am looking for to overwrite the content
            ));
        } catch (Exception $ex) {
            // error log
            return false;
        }

        if (!$updated) {
            // error log
            return false;
        }
        return true;
    }

}

我找到了解决办法

基本上,只需保存一个临时文件,并将其上载以覆盖现有文件

这是密码

<?php

class GCS_Driver {
    /** @var Google_Client $client */
    public $client;
    /** @var Google_Service_Storage $driver */
    public $driver;
    /** @var string $bucket */
    public $bucket;

    public function updateObject($objectPath,$content) {
        $updated = false;
        try {
            $temp = tempnam(sys_get_temp_dir(), 'gcs');
            $handle = fopen($temp, "r+b");
            fwrite($handle, $content);
            fseek($handle, 0);
            $postBody = new Google_Service_Storage_StorageObject();
            $postBody->setName($objectPath);
            $postBody->setUpdated(date("c"));
            $postBody->setGeneration(time());
            $size = @filesize($temp);
            $postBody->setSize($size);
            $ext = @pathinfo($objectPath,PATHINFO_EXTENSION);
            $ext = strtolower($ext);
            $mimeType = $this->getContentType($ext);
            $postBody->setContentType($mimeType);
            $chunkSizeBytes = 1 * 1024 * 1024;
            $this->client->setDefer(true);
            $request = $this->driver->objects->insert($this->bucket,$postBody,array(
                'name' => $objectPath,
                'predefinedAcl' => 'publicRead',
                'projection' => 'full',
            ));
            $media = new Google_Http_MediaFileUpload(
                    $this->client,
                    $request,
                    $mimeType,
                    null,
                    true,
                    $chunkSizeBytes
            );
            $media->setFileSize($size);
            $status = false;
            while (!$status && !feof($handle)) {
                $chunk = fread($handle, $chunkSizeBytes);
                $status = $media->nextChunk($chunk);
            }
            fclose($handle);
            unlink($temp);
            if ($status) {
                $updated = true;
            }
        } catch (Exception $ex) {
            // error log
            return false;
        }

        if (!$updated) {
            // error log
            return false;
        }

        // action log
        return true;
    }
}