How do I upload images to the Google Cloud Storage

2019-01-29 11:40发布

I'm currently utilizing a php app to upload images to the Google cloud storage platform, however, unlike on my local server, I am having tremendous trouble figuring out how to make this work.

Here is exactly what I am trying to do:

  1. Write the Path of the image to my Google cloud SQL
  2. Actually upload the image to the Google cloud storage platform
  3. write a script calling on the image, from the saved SQL path, to then post to my site

Can anyone point in the right direction?

Thanks!

1条回答
走好不送
2楼-- · 2019-01-29 12:12

Something like this worked for me with the form on GAE - upload photo from Form via php to google cloud storage given your folder permission are set...

// get image from Form
$gs_name = $_FILES["uploaded_files"]["tmp_name"]; 
$fileType = $_FILES["uploaded_files"]["type"]; 
$fileSize = $_FILES["uploaded_files"]["size"]; 
$fileErrorMsg = $_FILES["uploaded_files"]["error"]; 
$fileExt = pathinfo($_FILES['uploaded_files']['name'], PATHINFO_EXTENSION);

// change name if you want
$fileName = 'foo.jpg';

// put to cloud storage
$image = file_get_contents($gs_name);
$options = [ "gs" => [ "Content-Type" => "image/jpeg"]];
$ctx = stream_context_create($options);
file_put_contents("gs://<bucketname>/".$fileName, $gs_name, 0, $ctx);

// or move 
$moveResult = move_uploaded_file($gs_name, 'gs://<bucketname>/'.$fileName); 

The script to call the image to show on your site is typical mysqli or pdo method to get filename, and you can show the image with...

<img src="https://storage.googleapis.com/<bucketname>/<filename>"/>  
查看更多
登录 后发表回答