简体   繁体   中英

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. This is a CodeIgniter project. This function is within a controller and is able to get the image and store it in the project root's 'tmp/entries' folder.

Am I missing something? The file just doesn't upload to 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. I want to be able to upload this file to Google's servers from my dev server as well. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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