简体   繁体   English

谷歌应用引擎 - 上传的文件在谷歌云存储上不公开

[英]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.

相关问题 Google云端存储上的图片未设置为公开 - Google App Engine - Uploaded image does not set to public on Google Cloud Storage - Google App Engine Cloud Storage上的Google App Engine文件 - Google App Engine files on Cloud Storage 通过App Engine从Google Cloud Storage提供文件的上限? - Caps on serving files from Google Cloud Storage via App Engine? Google App Engine:如何在 PHP 循环中从 Google 云存储提供上传的图像 - Google App Engine: How to serve uploaded images in a PHP loop from google cloud storage Google App Engine PHP SDK-Google云存储-访问上载文件的内容 - Google App Engine PHP SDK - Google Cloud Storage - accessing contents of uploaded file PHP代码读取上传到谷歌应用引擎云存储桶的excel文件 - PHP code to Read excel file uploaded to google app engine cloud storage bucket 结合使用XMLReader和Google App Engine云存储 - Using XMLReader with Google App Engine Cloud Storage 具有云存储功能的Google App Engine无法在本地运行 - Google App Engine with Cloud Storage not Working Locally 将PHP getimagesize和imagecreate与Google App Engine和Google Cloud Storage结合使用 - Using PHP getimagesize and imagecreate with Google App Engine and Google Cloud Storage Google App Engine和PHP测试上传到Google Cloud Storage - Google App Engine and PHP Testing Upload to Google Cloud Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM