简体   繁体   English

在GAE PHP网站上提供Google Cloud Store中的文件

[英]Serving files from Google Cloud Store on a GAE PHP site

I have files in my Google Cloud store that I would like to serve embedded and for download on my site set up with GAE and PHP but I can't seem to get them to serve. 我在Google Cloud商店中有一些文件需要嵌入,并可以在使用GAE和PHP设置的网站上下载,但似乎无法提供它们。

I've been looking at: https://developers.google.com/appengine/docs/php/refdocs/files/api.cloud_storage.CloudStorageTools# \\google\\appengine\\api\\cloud_storage\\CloudStorageTools 我一直在查看: https : //developers.google.com/appengine/docs/php/refdocs/files/api.cloud_storage.CloudStorageTools# \\ google \\ appengine \\ api \\ cloud_storage \\ CloudStorageTools

The serve function doesn't seem to work for me, or likely I'm not employing it properly. serve功能似乎不适用于我,或者可能是我没有正确使用它。

Should I not just go serve($gs_filename); 我不应该只是去serve($gs_filename); to grab the file? 抢文件? Any help would be appreciated! 任何帮助,将不胜感激! Thanks. 谢谢。

UPDATE: 更新:

require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;

$bucket = 'raven-bucket';
$recording_name = '/194-14-02-2014rec.ogg';
$recording_data = CloudStorageTools::serve('gs://'.$bucket.$recording_name, ['content_type' => 'audio/ogg']);

This is what I've got so far, but I'm not sure what serve(); 到目前为止,这是我得到的,但是我不确定是什么serve(); is actually returning. 实际上正在返回。 How would I go about embedding this audio file in an HTML 5 audio element or making a download link? 我将如何将该音频文件嵌入HTML 5音频元素或进行下载链接?

UPDATE 2: This works! 更新2:可行!

<audio controls>
    <source src="test.php" type="audio/ogg">
</audio>

test.php: test.php:

<?php

require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
CloudStorageTools::serve('gs://raven-bucket/194-14-02-2014rec.ogg', 
    ['content_type' => 'audio/ogg']);

You can access files stored in cloud storage buckets natively, without using the CloudStorageTools. 您可以直接访问存储在云存储桶中的文件,而无需使用CloudStorageTools。

You will need to: 您将需要:

  1. Add permissions to the bucket / files for your App Engine apps service account eg your-app@appspot.gserviceaccount.com 将权限添加到您的App Engine应用服务帐户的存储桶/文件中,例如your-app@appspot.gserviceaccount.com
  2. Create a php.ini file and put in a google_app_engine.allow_include_gs_buckets directive specifying the buckets you wish to access (See https://developers.google.com/appengine/docs/php/config/php_ini#GAE_directives ) 创建一个php.ini文件,并放入google_app_engine.allow_include_gs_buckets指令,以指定您要访问的存储桶(请参阅https://developers.google.com/appengine/docs/php/config/php_ini#GAE_directives

  3. finally you can then just: 最后,您可以:

Include: 包括:

<? include("gs://your-bucket/file.php"); ?>

Or read the contents of the files: 或读取文件内容:

<? $content = file_get_contents("gs://your-bucket/file.txt"); ?>

CloudStorageTools::serve() is the right way to do this, for a few reasons 由于某些原因,CloudStorageTools :: serve()是执行此操作的正确方法

  1. It is more efficient - when you use CloudStorageTools::serve() the file is served by the App Engine infrastructure, so you do not occur the cost of reading the file in your instance. 效率更高-当您使用CloudStorageTools :: serve()时,该文件由App Engine基础结构提供服务,因此不会产生在实例中读取文件的开销。
  2. You can return files of any size using serve(). 您可以使用serve()返回任何大小的文件。 Reading the file using file_get_contents() and echoing that limits you to files of 32MB or less. 使用file_get_contents()读取文件并回显将文件限制为32MB或更小。

You can see the technique being used in our blogpost for generating dynamic sitemaps for WordPress - under the heading "Create a request handler", the code is essentially. 您可以在我们的博客文章中看到用于为WordPress生成动态站点地图的技术 -在“创建请求处理程序”标题下,代码本质上是。

Option 1. 选项1。

If you want to return the file '194-14-02-2014rec.ogg' in the bucket 'raven-bucket' as the response to a request then your php script looks like. 如果要在存储区“ raven-bucket”中返回文件“ 194-14-02-2014rec.ogg”作为对请求的响应,则您的php脚本应如下所示。

<?php

use google\appengine\api\cloud_storage\CloudStorageTools;
CloudStorageTools::serve('gs://raven-bucket/194-14-02-2014rec.ogg', 
                         ['content_type' => 'audio/ogg']);

Option 2. 选项2。

You want the user to download the file '194-14-02-2014rec.ogg' in the bucket 'raven-bucket', so that it is saved on their local computer. 您希望用户在存储区“ raven-bucket”中下载文件“ 194-14-02-2014rec.ogg”,以便将其保存在其本地计算机上。

<?php

use google\appengine\api\cloud_storage\CloudStorageTools;
CloudStorageTools::serve('gs://raven-bucket/194-14-02-2014rec.ogg', 
                         ['save_as' => '194-14-02-2014rec.ogg', 
                          'content_type' => 'audio/ogg']);

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

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