4

I've searched a lot (and even found some helpful links like this one Upload a file to Google Cloud Storage Bucket with PHP and Ajax) but I couldn't get it working. I need to upload a txt file to my google cloud storage bucket.

Here's my code:

gcp_storage.php:

<?php

if(isset($_FILES['file'])){
    $errors= array();
    // Authentication with Google Cloud Platform
    $client = new StorageClient(['keyFilePath' => 'cloudtest-2412**-a66e94ba56**.json']);
    $bucket = $client->bucket('cloudtest-2412**.appspot.com ');

    // Upload a file to the bucket.
    $file = pathinfo($_FILES['file']['name']);
    $prefix = uniqid('prefix_', true); //generate random string to avoid name conflicts
    $filename = $prefix . "." . $file['extension'];

    $bucket->upload(
      fopen($_FILES['file']['tmp_name'], 'r'),
      ['name' => $filename]
    );  
}

?>

html and js:

<html>
<form id="fileForm" action="" method="post" enctype="multipart/form-data">
    <div class="form-row">
        <br/>
        <div class="form-group">
            <label for="textFile">Text File: </label>
            <br/>
            <input type="file" name="file" id="textFile">
        </div>
    </div>
    <br>
    <button type="submit" class="btn btn-lg btn-primary"> Upload </button>
</form>

</html>

<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>

<script>

    $(document).on('submit', '#fileForm', function (e) {
    e.preventDefault();
    var data = new FormData($('#fileForm').get(0));
    $.ajax({
        type: 'POST',
        url: 'gcp_storage.php',
        data: data,
        processData: false,
        contentType: false,
        success: function ($data) {
            console.log('Succcess');
            $('#success').attr('hidden', false);
        }
    });
});

</script>

*It shows on my console success but when I go to my bucket on google cloud, nothing appears...

*I downloaded the .json file from the IAM page (the file is in the same directory as the other filse)

EDIT 1:

Don't know if this really helps, if I remove the use Google\Cloud\Storage\StorageClient, PHP throws me the error Class 'StorageClient' not found in /base/data/home/apps/i (when I put back the use code it just throws the 500 error, see below)

I've been changing the code and putting breakpoints to see if I find where the 500 error is coming from. If I remove everything from my PHP file and just put echo "Test", it works fine.

After putting some breakpoints at the original code, I found out that the 500 error comes from this lines of code:

$client = new StorageClient(['keyFilePath' => 'cloudtest-2412**-a66e94ba56**.json']);
$bucket = $client->bucket('cloudtest-2412**.appspot.com');

(The authentication code)

Any suggestions?

Thanks

Lucas Heise
  • 139
  • 3
  • 12
  • Are you using standard or flexible environment? – Noohone May 21 '19 at 14:49
  • Standard (I didn't really understand the difference between the two, so I chose Standard, but I wouldn't have a problem changing if needed) – Lucas Heise May 21 '19 at 16:03
  • What do the error logs show when the upload is attempted? – Grayside May 21 '19 at 16:31
  • In this code, it doesn't return any error, it shows "success" on my console. `success: function ($data) { console.log('Succcess'); $('#success').attr('hidden', false); }`. Where should I add some code to display the possible errors? – Lucas Heise May 21 '19 at 17:08
  • Actually I managed to get to display an error message, now it shows error 500 (server) @Grayside – Lucas Heise May 22 '19 at 00:26
  • The error message we need is the PHP error. The 500 number is an HTTP error code indicating the PHP script failed. If the PHP code is executing at all, one of the gcs library interactions is failing and likely surfacing an exception. – Grayside May 22 '19 at 00:57
  • Hey @Grayside, can you see my edit in the question? At least I found out which lines of the PHP code are giving the 500 error and tht PHP error – Lucas Heise May 22 '19 at 11:47
  • The official documentation on this is https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-php, and in looking at the code itself, I'd guess it's related to authentication, if so this might help: https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application. We won't know for sure without reviewing the specific error message caused by the breaking line of code, which you can get by enabling display_errors https://stackoverflow.com/a/17693462/38408. – Grayside May 22 '19 at 18:14
  • I already enabled display_errors, now I'm trying to only run the php code (without calling it from the ajax) and it gives me the class not found error, I saw that I need to install it on composer, but I never really used this so I'm kinda lost... – Lucas Heise May 22 '19 at 18:17
  • I think I'm going to try to use this on Java (android), currently I call the PHP file from volley on android, I guess I'll just try to do directly from android (Java). Thanks for all the help... – Lucas Heise May 22 '19 at 18:37

1 Answers1

0

I think, we can see a permission error. In most cases, an internal 500 server error is due to incorrect permission in one or more files or folders.

Please try with this steps: https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application