简体   繁体   English

使用PHP上传到Google Cloud Storage时没有文件名

[英]No filename when uploading to Google Cloud Storage in PHP

I'm programatically uploading pdf files and want to set the filename. 我正在以编程方式上传pdf文件,并希望设置文件名。 When I upload a file via POST, the filename property is set automatically. 通过POST上传文件时,文件名属性会自动设置。 However when using the method below, it renames it something like "s9QmBqJPuiVzWbySYvHVRg==" and there is no filename. 但是,当使用下面的方法时,它将重命名为“ s9QmBqJPuiVzWbySYvHVRg ==”,并且没有文件名。

$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. 如果我下载然后添加.pdf作为扩展名,它将以原始pdf格式打开,因此实际文件本身已经上传了,但是只是文件名和.pdf扩展名。 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. 我已经看到了解决方案 ,但是它在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? 在本地测试文件名与在Google App Engine上进行实时测试时,文件名的处理方式是否有所不同?

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. 因此,我继续进行部署,结果发现在本地测试文件名和在Google App Engine上测试文件名时,文件名的处理方式不同。

So if you're testing locally, you can test if the object uploaded via GAE Launcher --> SDK Console --> Blobviewer. 因此,如果您在本地进行测试,则可以测试对象是否通过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. 但是,一旦您部署到GAE上并进行测试,该文件就会以正确的文件名上传到GCS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM