0

Very new to this and have been trying to get it to work. I have a bucket called app. I have followed the instructions to give my app permissions to the GCS (https://developers.google.com/appengine/docs/python/googlestorage/index#Prerequisites). It seems to be setup correctly.

I followed How do I upload images to the Google Cloud Storage from PHP form? which has worked for others. I haven't really diverted from it.

I get move_uploaded_file returning TRUE, but no file in GCS.

The output is "MoveResult did succeed". Anyone see anything I am doing wrong. Am I missing some setup code or something?

Here is the code:

if(!is_uploaded_file($_FILES['file']['tmp_name'])) { 
     $profile_pic_path='default';
}
else { //file exists
    $gs_name = $_FILES["file"]["tmp_name"]; 

    $fileType = $_FILES["file"]["type"]; 
    $fileSize = $_FILES["file"]["size"]; 
    $fileErrorMsg = $_FILES["file"]["error"]; 
    $fileExt = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

    // change name if you want
    $fileName = 'profile_pic.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://app/".$fileName, $gs_name, 0, $ctx);
    if(!move_uploaded_file($gs_name, 'gs://app/'.$fileName)){
        echo json_encode(array('success' => 0,'error_message' => "MoveResult did not succeed"));
        exit;
    }
    else{
        //Get image key in or path or what not. THIS IS THE NEXT STEP
        //$profile_pic_path=<something>;
        echo json_encode(array('success' => 1,'info_message' => "MoveResult did succeed"));
        exit;
    }

}

Community
  • 1
  • 1
lr100
  • 648
  • 1
  • 9
  • 29
  • Anyone have some code to just "touch" a text file on GCS so I can see if it something with the setup? – lr100 Jul 18 '14 at 17:17
  • You can use gsutil https://developers.google.com/storage/ or Developer console http://console.developers.google.com to verify the move. – Mars Jul 21 '14 at 20:28
  • Mars....there is nothing in there. That is why I am asking why this code is not working. – lr100 Jul 21 '14 at 20:31
  • Do you see the file pointed to by $_FILES["file"]["tmp_name"] in GCS? – Mars Jul 21 '14 at 20:58
  • No. I do not. I am looking in the bucket called "app". – lr100 Jul 21 '14 at 20:59
  • What's the value of $_FILES["file"]["tmp_name"]? – Mars Jul 21 '14 at 21:04
  • /var/tmp/phpLXVuAN. But it changes everytime I do it but the /var/tmp/ part stays the same. – lr100 Jul 22 '14 at 14:01
  • The path you posted suggested that you're trying to run this locally? Also you should really use CloudStorageTools::createUploadUrl() to generate the upload link. (See https://developers.google.com/appengine/docs/php/googlestorage/user_upload) – Mars Jul 22 '14 at 18:18
  • My app engine is local, but the GCS is indeed is not. I am able to use Cloud SQL (not local) with my app engine (local). – lr100 Jul 22 '14 at 18:20
  • To achieve that, you'll use file_put_contents() instead of move_uploaded_file(). – Mars Jul 22 '14 at 18:23
  • In my source I have that commented out because I believe it didn't work either. I will give it another try just in case :) – lr100 Jul 22 '14 at 18:24
  • If it fails, it's most likely due to permission problem. You'll need to make the destination "publicly writable" in this case, which is not a recommended practice. You'd be much better off deploying the app to App Engine and runs it off from there instead. – Mars Jul 22 '14 at 18:32
  • Switched it to use file_put_contents() and it returns 18, not FALSE. But no file in https://console.developers.google.com/project//storage/app/ – lr100 Jul 22 '14 at 18:43
  • Yes, it's not expected to work unless you make the bucket publicly writable. – Mars Jul 22 '14 at 20:13
  • So....I open up all my permissions using "gsutil acl set public-read-write gs://app". I verified with "gsutil acl get gs://app". I was then able to use gsutil to mv a file using "gsutil mv test.txt gs://app". However, my php code is still saying it worked (TRUE is move_uploaded_file is being used, 18 if file_put_contents is used) but no file on GCS. Any suggestions? – lr100 Jul 23 '14 at 15:35

0 Answers0