【问题标题】:Upload file using PHP from Compute Engine to Cloud Storage使用 PHP 将文件从 Compute Engine 上传到 Cloud Storage
【发布时间】:2014-07-17 12:47:34
【问题描述】:

似乎没有示例应用程序或演示使用 PHP 将文件从托管在 Compute Engine 上的 PHP 应用程序上传到谷歌云存储。

我正在努力对 google-api-php-client 库进行逆向工程,以弄清楚它应该如何工作。

我查看了 github 上的 storage-getting-started-php,但脚本中似乎没有任何地方可以上传。

我可以使用 JSON 库并编写我自己的 php 文件处理库,但如果代码已经写在 php 客户端库中,这似乎是不必要的。

对于这个问题的任何帮助将不胜感激。我正在将一个非常高流量的站点迁移到计算引擎。

【问题讨论】:

  • 我已经联系了 Google 支持,但显然他们也没有这方面的任何信息。他们正在询问开发人员。如果他们能给我一个答案,我会在这里发布答案。

标签: google-cloud-storage google-compute-engine


【解决方案1】:

从 github 查看此链接。 https://github.com/google/google-api-php-client/tree/master/examples。我的理解也是,您需要获得授权令牌才能使用 PHP 上传文件。

【讨论】:

  • 我可能错了,但我仔细查看了这些示例,但找不到用于谷歌云存储的示例。在处理文件时,它们都是用于谷歌驱动器的。
【解决方案2】:

我一直在联系 Google 支持。他们给了我这个代码。不过我还没有机会测试它。

<?php
include 'lib/cms.php';
require_once('config.php');
require_once(MVC_ROOT . 'models/App.php');

ini_set("memory_limit", "-1M");
ini_set("max_input_time", "-1");
ini_set("max_execution_time", "-1");
set_time_limit(0);
ignore_user_abort();

$copyOrMove =  (SYSTEM_PLATFORM == 'gogrid') ? 'cp' : 'mv';

$cms = new CMS();
$dbh = $cms->dbh;

$data = $_POST['data'];
//$appcode = $cms->safeInput($_REQUEST['app']);
$appcode = $cms->getAppcode();
$type = $_REQUEST['type'];

$appdir = '../apps/' . $appcode;
if (!file_exists($appdir)) mkdir($appdir);
if (!file_exists($appdir . '/images')) mkdir($appdir . '/images');
if (!file_exists($appdir . '/images/org')) mkdir($appdir . '/images/org');
if (!file_exists($appdir . '/images/gallery')) mkdir($appdir . '/images/gallery');
if (!file_exists($appdir . '/images/30')) mkdir($appdir . '/images/30');
if (!file_exists($appdir . '/images/75')) mkdir($appdir . '/images/75');
if (!file_exists($appdir . '/images/200')) mkdir($appdir . '/images/200');
if (!file_exists($appdir . '/images/resources')) mkdir($appdir . '/images/resources');
if (!file_exists($appdir . '/attachments')) mkdir($appdir . '/attachments');


if ($type == "image") {

    $filename = 'homepage-' . date("Ymd-His") . '.png';
    $dir = "../apps/" . $appCode . "/images";
    if (!is_dir($dir)) {
        mkdir($dir, 0755, true);
    }
    //first lets delete the old ones...
    foreach (glob("../apps/" . $appcode . "/images/homepage*.png") as $killfilename) {
        echo "$killfilename size " . filesize($killfilename) . "\n";
        unlink($killfilename);
        exec("gsutil -m rm gs://gs.appbuild.io/apps/$appcode/images/homepage*.png");
    }

    //$filename='homepage.png';
    $file = "../apps/$appcode/images/" . $filename;

    $uri = substr($data, strpos($data, ",") + 1);
    file_put_contents($file, base64_decode($uri));

    $dir = "../apps/" . $appcode . "/images/resources";
    resizeImage($file, 640, 960, $dir . "/Default@2x.png");
    resizeImage($file, 640, 1136, $dir . "/Default-568h@2x.png");
    resizeImage($file, 320, 480, $dir . "/Default.png");

    $pathArray = array("../apps/$appcode/images/resources/Default@2x.png","../apps/$appcode/images/resources/Default-568h@2x.png","../apps/$appcode/images/resources/Default.png");

    exec("gsutil $copyOrMove ../apps/$appcode/images/$filename gs://gs.appbuild.io/apps/$appcode/images/");

    exec("gsutil -m $copyOrMove ". implode(' ',$pathArray) ." gs://gs.appbuild.io/apps/$appcode/images/resources/");

    $sql = "INSERT INTO appearance SET appearance_background_image='$filename', app_code='$appcode' ON DUPLICATE KEY UPDATE appearance_background_image='$filename'";
    $stmt = $dbh->query($sql);
    $dbh = null;

    // return the filename
    echo URL_CDN_ROOT . "apps/$appcode/images/" . $filename;
} else if ($type == "icon") {
    $size = $_REQUEST['size'];
    $filename = 'icon_' . $size . '.png';

    $dir = "../apps/" . $appcode . "/images/resources";

    if (!is_dir($dir)) {
        mkdir($dir, 0755, true);
    }

    $file = $dir . "/$filename";
    $uri = substr($data, strpos($data, ",") + 1);
    file_put_contents($file, base64_decode($uri));

    resizeImage($file, 57, 57, $dir . "/icon_57.png");
    resizeImage($file, 57, 57, $dir . "/icon.png");
    resizeImage($file, 72, 72, $dir . "/icon_72.png");
    resizeImage($file, 114, 114, $dir . "/icon_114.png");
    resizeImage($file, 114, 114, $dir . "/icon@2x.png");
    resizeImage($file, 120, 120, $dir . "/icon_120.png");
    resizeImage($file, 175, 175, $dir . "/icon_175.png");
    resizeImage($file, 1024, 1024, $dir . "/icon_1024.png");

    $filepaths = array($filename, "icon_57.png", "icon.png", "icon_72.png", "icon_114.png", "icon@2x.png", "icon_120.png", "icon_175.png", "icon_1024.png");

    $fromArray = array();
    foreach ($filepaths as $filenm){
        $fromArray[] = "../apps/$appcode/images/resources/$filenm";
    }

    exec("gsutil -m $copyOrMove ". implode(' ', $fromArray) ." gs://gs.appbuild.io/apps/$appcode/images/resources/");   

    $app = new App($appcode);
    $app->setAppIcon(implode(',', $filepaths));

    echo URL_CDN_ROOT . "apps/$appcode/images/resources/$filename";
}

if (SYSTEM_PLATFORM == 'computeengine' && $appcode != '') delTree("../apps/$appcode"); 

function resizeImage($filename, $new_width, $new_height, $dirname)
{
    list($width, $height) = getimagesize($filename);
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefrompng($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    //$newfilename = $dir . "/icon_" . $new_width . ".png";
    imagepng($image_p, $dirname);
}

function delTree($dir) { 
    $files = array_diff(scandir($dir), array('.','..')); 
    foreach ($files as $file) { 
        (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file"); 
    } 
    return rmdir($dir); 
} 

?>

【讨论】:

  • 请注意,这是将整个文件加载到内存中。这可能需要分成几个部分并上传每个部分。
猜你喜欢
  • 2021-06-19
  • 2020-04-04
  • 2014-11-22
  • 2021-11-05
  • 2019-04-08
  • 2015-09-27
  • 2023-03-14
  • 1970-01-01
  • 2013-11-02
相关资源
最近更新 更多