简体   繁体   English

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

[英]Upload an image to Google Cloud Storage with PHP?

i just can't get this code to work. 我只是无法使此代码正常工作。 I'm getting an image from the URL and storing it in a temporary folder so I can upload it to a bucket on Google Cloud Storage. 我从URL获取图像并将其存储在一个临时文件夹中,因此可以将其上传到Google Cloud Storage上的存储桶中。 This is a CodeIgniter project. 这是一个CodeIgniter项目。 This function is within a controller and is able to get the image and store it in the project root's 'tmp/entries' folder. 此功能在控制器内,能够获取图像并将其存储在项目根目录的“ tmp / entries”文件夹中。

Am I missing something? 我想念什么吗? The file just doesn't upload to Google Cloud Storage. 该文件只是没有上传到Google Cloud Storage。 I went to the Blobstore Viewer in my local App Engine dev server and notice that there is a file but, it's empty. 我去了本地App Engine开发服务器中的Blobstore Viewer,注意到有一个文件,但是它是空的。 I want to be able to upload this file to Google's servers from my dev server as well. 我也希望能够从我的开发服务器将此文件上传到Google的服务器。 The dev server seems to overwrite this option and save all files locally. 开发服务器似乎会覆盖此选项,并将所有文件保存在本地。 Please help. 请帮忙。

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

As you noticed, the dev app server emulates Cloud Storage locally. 如您所见,开发应用程序服务器在本地模拟Cloud Storage。 So, this is the intended behaviour-- and it lets you test without modifying your production storage. 因此,这是预期的行为,它使您无需修改​​生产存储即可进行测试。 If you run the deployed app you should see the writes actually going to your GCS bucket. 如果您运行已部署的应用程序,则应该看到实际写入GCS存储桶的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM