[英]Google app-engine - Uploaded files not public on google cloud storage
I have an app that lets users upload an image, and I want other users to be able to see that image. 我有一个应用程序,允许用户上传图像,我希望其他用户能够看到该图像。
I am using the CloudStorageTools::createUploadUrl method to allows users to upload via POST. 我使用CloudStorageTools :: createUploadUrl方法允许用户通过POST上传。 I am then taking the uploaded file and using move_uploaded_file to store it on Google Cloud Storage.
然后,我将获取上传的文件并使用move_uploaded_file将其存储在Google云端存储上。
Then, I am using CloudStorageTools::getPublicUrl to get the URL for the file. 然后,我使用CloudStorageTools :: getPublicUrl来获取文件的URL。 It returns the correct URL, but the file is not publicly accessible so I get a XML error response "AccessDenied".
它返回正确的URL,但该文件不可公开访问,因此我收到XML错误响应“AccessDenied”。
I know this is because the file isn't shared publicly, but I'm not sure how to make it public after it gets upload. 我知道这是因为该文件未公开共享,但我不确定如何在上传后将其公开。
I tried passing in a "acl=public-read" option to createUploadUrl, but it returned an error of unknown option. 我尝试将“acl = public-read”选项传递给createUploadUrl,但它返回了未知选项的错误。
I see an example of how todo this on this page: https://developers.google.com/appengine/docs/php/googlestorage/public_access however, my workflow requires me to allow normal POST uploads using the createUploadUrl method as shown here: https://developers.google.com/appengine/docs/php/googlestorage/user_upload 我在此页面上看到了一个示例: https : //developers.google.com/appengine/docs/php/googlestorage/public_access但是,我的工作流程要求我使用createUploadUrl方法允许正常的POST上传,如下所示: https://developers.google.com/appengine/docs/php/googlestorage/user_upload
How do I make user uploaded files automatically publicly accessible? 如何让用户上传的文件自动公开访问?
Got this to work using file_put_contents with a stream. 使用带有流的file_put_contents可以使用此功能。
$file = $_FILES['myfile']
$storeAt = 'gs://bucketname/filename.jpg
Old code 旧代码
$saved = move_uploaded_file($file['tmp_name'],$storeAt);
New code, sets permissions and mime type 新代码,设置权限和mime类型
$options = array('gs'=>array('acl'=>'public-read','Content-Type' => $file['type']));
$ctx = stream_context_create($options);
$saved = file_put_contents($storeAt, file_get_contents($file['tmp_name']), 0, $ctx);
Go me! 去吧!
As of September 23 2014 , I managed to upload files to Google Storage Cloud via appEngine and set the acl
to public-read
(everyone) like this: 截至2014年9月23日 ,我设法通过appEngine将文件上传到Google Storage Cloud,并将
acl
设置为public-read
(everyone),如下所示:
form.php : form.php :
<?
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
$options = [ 'gs_bucket_name' => 'yourbucketname' ];
$upload_url = CloudStorageTools::createUploadUrl('/upload_handler.php', $options);
?>
<form action="<?php echo $upload_url?>" enctype="multipart/form-data" method="post">
Files to upload: <br>
<input type="file" name="userfile" size="40">
<input type="submit" value="Send">
</form>
upload_handler.php : upload_handler.php :
<?php
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
$fileName = 'gs://yourbucket/'.$_FILES['userfile']['name'];
echo $fileName."<br>";
$options = array('gs'=>array('acl'=>'public-read','Content-Type' => $_FILES['userfile']['type']));
$ctx = stream_context_create($options);
if (false == rename($_FILES['userfile']['tmp_name'], $fileName, $ctx)) {
die('Could not rename.');
}
$object_public_url = CloudStorageTools::getPublicUrl($fileName, true);
echo $objectPublicUrl."<br>";
//header('Location:' .$objectPublicUrl); // redirects the user to the public uploaded file, comment any previous output (echo's).
?>
To be able to accept big files, I've also created a custom php.ini
on the base directory of my app: 为了能够接受大文件,我还在我的应用程序的基本目录中创建了一个自定义的
php.ini
:
php.ini : php.ini :
; This is a simple php.ini file on App Engine
; It enables output buffering for all requests by overriding the
; default setting of the PHP interpreter.
output_buffering = "On"
max_file_uploads = 0
upload_max_filesize = 100M
session.gc_maxlifetime = 10000
app.yaml , sets all images files as static and .php's
as runnable, it looks like this: app.yaml ,将所有图像文件设置为静态,将
.php's
为runnable,它看起来像这样:
application: myapp
version: 1
runtime: php
api_version: 1
threadsafe: true
handlers:
# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
static_files: \1
upload: .+\.(gif|png|jpg)$
application_readable: true
# Serve php scripts.
- url: /(.+\.php)$
script: \1
Hope it helps anyone else ;) 希望它可以帮助其他人;)
the option max_file_uploads can't be 0. max_file_uploads选项不能为0。
max_file_uploads = 10 (or whatever you want) max_file_uploads = 10(或任何你想要的)
None of the options listed here seem to work anymore. 此处列出的选项似乎都不再起作用。
The only way I've been able to get public access for files it to create a bucket with public access and use that for uploaded files. 我能够获取文件的公共访问权限的唯一方法是创建一个具有公共访问权限的存储桶,并将其用于上传的文件。
gsutil defacl set public-read gs://bucket
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.