使用PHP将文件从Compute Engine上传到Cloud Storage [英] Upload file using PHP from Compute Engine to Cloud Storage

查看:86
本文介绍了使用PHP将文件从Compute Engine上传到Cloud Storage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There seems to be no example application or demonstration of uploading files using PHP to google cloud storage from a PHP application hosted for example on Compute Engine.

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

I'm struggling to reverse engineer the google-api-php-client library to figure out how it's supposed to work.

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

I have looked at storage-getting-started-php on github but there doesn't seem to be anywhere in the script where there is an upload.

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

I could use the JSON library and write my own php file handling library but this seems unnecessary if the code has already been written in the php client library.

在此问题上的任何帮助将不胜感激.我正在将一个真正高流量的站点迁移到计算引擎.

Any help on this issue would be greatly appreciated. I'm migrating a really high traffic site to compute engine.

推荐答案

我已经与Google支持人员联系.他们给了我这段代码.我还没有机会进行测试.

I have been in contact with google support. They've given me this code. I haven't had the chance to test it yet though.

<?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); 
} 

?>

这篇关于使用PHP将文件从Compute Engine上传到Cloud Storage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆