使用 PHP 将图像上传到 Google Cloud Storage


Upload an image to Google Cloud Storage with PHP?

我只是无法让这段代码工作。我正在从URL获取图像并将其存储在临时文件夹中,以便我可以将其上传到Google Cloud Storage上的存储桶。这是一个CodeIgniter项目。此功能位于控制器中,能够获取图像并将其存储在项目根目录的"tmp/entryries"文件夹中。

我错过了什么吗?该文件只是没有上传到谷歌云存储。我访问了本地 App Engine 开发服务器中的 Blobstore 查看器,注意到有一个文件,但它是空的。我也希望能够将此文件从我的开发服务器上传到 Google 的服务器。开发服务器似乎覆盖了此选项并将所有文件保存在本地。请帮忙。

public function get()
{
    $filenameIn  = 'http://upload.wikimedia.org/wikipedia/commons/1/16/HDRI_Sample_Scene_Balls_(JPEG-HDR).jpg';
    $filenameOut = FCPATH . '/tmp/entries/1.jpg';
    $contentOrFalseOnFailure   = file_get_contents($filenameIn);
    $byteCountOrFalseOnFailure = file_put_contents($filenameOut, $contentOrFalseOnFailure);
    $options = [ "gs" => [ "Content-Type" => "image/jpeg" ]];
    $ctx = stream_context_create($options);
    file_put_contents("gs://my-storage/entries/2.jpg", $file, 0, $ctx);
    echo 'Saved the Image';
}

正如你所注意到的,开发应用服务器在本地模拟云存储。因此,这是预期的行为 -- 它允许您在不修改生产存储的情况下进行测试。如果您运行已部署的应用程序,您应该会看到写入操作实际进入您的 GCS 存储桶。