Search code examples
phpgoogle-app-enginefilenamesgoogle-cloud-storageblobstore

No filename when uploading to Google Cloud Storage in PHP


I'm programatically uploading pdf files and want to set the filename. When I upload a file via POST, the filename property is set automatically. However when using the method below, it renames it something like "s9QmBqJPuiVzWbySYvHVRg==" and there is no filename.

$gs_file='gs://<bucket-name>/proposal.pdf';
$options = ['gs' => ['Content-Type' => 'application/pdf']];
$ctx = stream_context_create($options);
file_put_contents($gs_file,$content,0,$ctx);

If I download then add .pdf as extension, it opens as the original pdf fine, so the actual file itself has uploaded ok, but its just the filename and .pdf extension. Is there a way to have the file retain its original filename when uploaded?

I have seen a solution to this, but its in python.

Note: I am testing this out on my local environment before deploying. Is the handling of filenames different when testing it locally vs live on Google App Engine?


Solution

  • So I went ahead and deployed it and turns out the filename is handled differently when testing it locally versus when on Google App Engine.

    So if you're testing locally, you can test if the object uploaded via GAE Launcher --> SDK Console --> Blobviewer. Note that the filename will be empty and the key will be a set of alphanumerics.

    But once you deploy onto GAE and test, the file will upload to GCS with the correct filename.