截至 2014 年 9 月 23 日,我设法通过 appEngine 将文件上传到 Google Storage Cloud,并将 acl 设置为 public-read(所有人),如下所示:
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:
<?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).
?>
为了能够接受大文件,我还在我的应用程序的基本目录中创建了一个自定义 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,将所有图片文件设置为静态,.php's 设置为可运行,如下所示:
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
希望它可以帮助其他人;)