0

I need to upload larger file on google stoarge.

Actually, what I'm doing is to create an URL in this way:

$url = $object->beginSignedUploadSession([
    "options" => [
        "x-goog-x-upload-content-type" => "$type",
        "x-goog-content-type" => "$type",
    ]
]);

On frontend, I'm fetching this URL and make a PUT request with file

let type = files[i].type;
$.ajax({
    type: "GET",
    url: "/getUrl",
    data: dataToPass,
    success: function (json) {
        var jsonParse = JSON.parse(json);
        var url = jsonParse.url;
        var formData = new FormData();

        formData.append('file', files[i]);

        var ajaxRequest = $.ajax({
            type:'PUT',
            url: url,
            data: formData,
            processData: false,
            contentType: type,
            cache: false,
                xhr: function () {
                    myXhr = $.ajaxSettings.xhr();
                    if (    myXhr.upload) {
                        myXhr.upload.addEventListener('progress', thisIsAFunction, false);
                    }
                    return  myXhr;
                },

Upload seems works, but on my storage file is corrupted. enter image description here

Actually the filetype is mp4, but storage save it as applicastion/octet-stream. Filesize is correct but file not working.

This is the PUT request payload enter image description here

tidpe
  • 325
  • 3
  • 15

2 Answers2

1

Solved by passing directly the file to Ajax, without appending it to the form data.

var ajaxRequest = $.ajax({
                type:'PUT',
                url: url,
                data: files[i],
                processData: false,
                contentType: type,
                cache: false, ....code goes on...

Correct way to pass contentType for the bucket is:

$url = $object->beginSignedUploadSession([
        "contentType" => $type,
        ...... 
    ])
tidpe
  • 325
  • 3
  • 15
0

This happens when there is no contentType defined for the file during the upload.

This shouldn't be happening since you have a extension for the file in the filename so this lead me to believe that even though you are providing a contentType, it has an invalid value and saves it as a file without extension.

So in order to fix it I would advise you to log and check the value of your type variable, as this is likely the cause of the issue. The proper value that it need to have in order to upload the file as a .mp4 video is video/mp4.

Ralemos
  • 5,571
  • 2
  • 9
  • 18
  • Solved as wrote. Just to be curious, there is a way to specify content-length-range and a success_action_redirect while generating beginSignedUploadSession? – tidpe Aug 09 '21 at 13:05
  • 1
    Not with a `content-length-range`, but you can use a specific `content-length` for setting a top limit, as described in this [community answer](https://stackoverflow.com/a/66841197/12857703), as per the `success_action_redirect` I could not find anything in the documentation nor community posts so can't say for sure. – Ralemos Aug 09 '21 at 13:12
  • Adding content-length does not block user to upload larger file :/ maybe some syntax is wrong? https://pastebin.com/MDLbSkSV – tidpe Aug 09 '21 at 13:47
  • Tried removing "options" from array and just use "headers" with valuse, still nothing – tidpe Aug 09 '21 at 14:57
  • I think this might be worth asking in a separate question then :) – Ralemos Aug 10 '21 at 12:17