简单更新 Google 云存储中的文本内容

Simple update a text content in Google Cloud Storage

我是 Google Cloud Storage 的新用户,请多多包涵。

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

我正在使用 google-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;
    }
}

欢迎任何新的反馈。