Google Cloud Storage

Discussion in 'jQuery' started by Jeremy Benson, Mar 25, 2023.

  1. #1
    Anyone ever get Jquery uploads to work with their HTML form? I'm using PHP SDK along with this, sending it as FormData. The problem is I keep getting a errors. If I just send the form they supply, signed by PHP api it works fine. When I try to incorporate jquery with any dataType other than jsonp I get CORS error. With jsonp I get 403. Which is a credentials error.

    I've set ths header on the top:

    header('Access-Control-Allow-Origin: http://localhost'); 
    Code (markup):
    My jquery is like this:

    $.ajax({
                xhr: function() {
                    var xhr = new window.XMLHttpRequest();
                    xhr.upload.addEventListener("progress", function(evt) {
                        if (evt.lengthComputable) {
                            var percentComplete = ((evt.loaded / evt.total) * 100);
                            document.querySelector("#uploadFileProgressBar" + fileStack[currentFileIndex]).setAttribute("value", percentComplete);
    
    
                        }
                    }, false);
                    return xhr;
                },
                type: 'POST',
                contentType: 'multipart/form-data',
                dataType: 'jsonp',
                processData: false, //prevent jQuery from automatically transforming the data into a query string
                url: $('#uploadForm').attr('action'),
                data: formObj,
                //Add this line to resolve cross origin error
                headers: {
                    'Access-Control-Allow-Origin': '*',
                },
                cache: false,
                beforeSend: function() {
    Code (markup):
    I set up the form like this:

    require('../vendor/autoload.php');
                use Google\Cloud\Storage\StorageClient;
                           
                putenv('GOOGLE_APPLICATION_CREDENTIALS='. '../../Yen7AaH9rb/'.$GoogleStoragePrivateKey.'.json');
           
                $storage = new StorageClient([
                    'projectId' => $GoogleStorageId/* your GCP projectId here */,
                    'keyFilePath' => '../../Yen7AaH9rb/'.$GoogleStoragePrivateKey.'.json'/* your GCP keyFilePath here */,
                ]);
               
                 $bucketName = $jobFilesBucket;
                 $objectName = 'upload_file'; /*Name to give file uploaded to GCS */
    
                $bucket = $storage->bucket($bucketName);
       
                $response = $bucket->generateSignedPostPolicyV4(
                    $objectName,
                    new \DateTime('10 min'),
                    [
                        'fields' => [
                            'x-goog-meta-test' => 'data'
                           
                        ]
                    ]
                );
               
                $url = $response['url'];
                $output = "<form id=\"uploadForm\" action='$url' method='POST' enctype='multipart/form-data'>" . PHP_EOL;
                foreach ($response['fields'] as $name => $value) {
                    $output .= "  <input name='$name' value='$value' type='hidden'/>" . PHP_EOL;
                }
                $output .= "  <input type='file' name='file'/><br />" . PHP_EOL;
                $output .= "  <input type='submit' value='Upload File' name='submit'/><br />" . PHP_EOL;
                $output .= '</form>' . PHP_EOL;
    
                echo $output;
    Code (markup):

     
    Jeremy Benson, Mar 25, 2023 IP