Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Storage File Upload - Requested page not found. [404]

I am trying to upload a file to Google Cloud Storage using Json API. I am posting the request to below url with the file attachment.

https://www.googleapis.com/upload/storage/v1/b/mybucket/o?uploadType=media&name=solarsystemlrg.png

But I am getting Requested page not found. [404] Error. When I use the below Url, I am able to list the images stored in the bucket (Just to be sure that there are no access issues and bucket name is correct).

https://www.googleapis.com/storage/v1/b/mybucket/o

I have set the headers.

'Authorization' :'Bearer ' + accessToken,
'Content-Type' : contentType,
'Content-Length' : inputFile.size

Please find the code snippet below.

        $.ajax({
        url: targetUrl,
        headers: reqHeaders,
        type: 'POST',
        data: reqFormWithDataFile,
        async: false,
        cache: false,
        dataType: 'jsonp',
        useDefaultXhrHeader: false,
        processData: false,
        success: function (response) {

                alert('File Upload Successful');

        },
        error: function (jqXHR, exception) {
            var msg = '';
            if (jqXHR.status === 0) {
                msg = 'Not connect.\n Verify Network.';
            } else if (jqXHR.status == 404) {
                msg = 'Requested page not found. [404]';
            } else if (jqXHR.status == 500) {
                msg = 'Internal Server Error [500].';
            } else if (exception === 'parsererror') {
                msg = 'Requested JSON parse failed.';
            } else if (exception === 'timeout') {
                msg = 'Time out error.';
            } else if (exception === 'abort') {
                msg = 'Ajax request aborted.';
            } else {
                msg = 'Uncaught Error.\n' + jqXHR.responseText;
            }
            alert(msg);
        }
    });

Do I need to use a different URL? Or is there any other setting required? Please help.

like image 940
Lalit Rane Avatar asked Nov 08 '22 09:11

Lalit Rane


1 Answers

That's the right URL, so long as you replace mybucket with your actual bucket name. After reading through jquery.ajax(), I emulated what I think is the behavior by adding &callback=foo, due to dataType: 'jsonp' and &_=1474410231688 because cache: false, and issued the request via curl and that produced a valid object with a valid callback. Can you log the entire jqXHR to see more of the status?

like image 82
Nathan Herring Avatar answered Nov 15 '22 09:11

Nathan Herring