duanru6816 2014-10-15 00:25
浏览 97
已采纳

Appengine Cloud Storage上传图像并存储在存储中

I have a problem. any help is welcome.

I am developing a solution that need to use google apis like cloud storage, Drive, etc.

In the clasic profile page I have to upload picture profile. then I use a angularjs to post data to my appengine/php/yii

formPHP:

`

<?php
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
$options = [ 'gs_bucket_name' => 'seektrabajo' ];
$upload_url = CloudStorageTools::createUploadUrl('/perfiles/subirFotoPerfil', $options);
?>
<input type="file" name="file" 
       nv-file-select="uploader.uploadAll()" 
       uploader="uploader"   />

`

and when submit send data to google and google to yii ajax service:

`

public function actionSubirFotoPerfil(){
     $answer = array('answer'=>'Incompleted','serverFile'=>'sinfoto.png');
     if(!empty( $_FILES )){
         $filename = $_FILES['file']['name'];
         $gs_name =  $_FILES['file']['tmp_name'];
         move_uploaded_file($gs_name, 'gs://seektrabajo/'.$filename);
         $answer = array( 'answer' => 'File transfer completed','serverFile' => $filename);
     }
     echo json_encode( $answer );
     Yii::app()->end();
 }

`

the problem is that never save the file uploaded to my bucket on cloud storage. this not work for development/local mode and either appengine deployed. in appengine land, I get this error:

I just get permision to bucket:

gsutil acl ch -u user@gmail.com:FULL_CONTROL gs://seektrabajo

the user@gmail.com y get from my google console/ apis-autentications/ Email

![web browser console error][1]

http://i.stack.imgur.com/noKnh.png

Somebody have an idea? Thanks.

  • 写回答

1条回答 默认 最新

  • douxianglu4370 2014-12-20 22:44
    关注

    Why are you uploading the files to a static handler then uploading them to GCS? Use createUploadUrl() to template the <form> you serve. Provide a callback URL on your own app to the function and once the file is uploaded, a request will go to your app with the rest of the parameters of the form, the metadata of the file, etc...

    With the pattern you are using, there is not only the headache of trying to figure out how to stream the data correctly, there's the simple fact that you're handling all that read/write on your instance, which is surely not a good idea if you intend to use this in production code where you pay for the used resources.

    In short, there's no reason to use this pattern. In a traditional web app, you would post the file form to your own server on a route and do like you are in your example code (minus forwarding to GCS), but with App Engine you need to think a little different than the traditional ways of web development on a single VM you purchase and host a PHP runtime on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站