首页 文章

图片上传到谷歌桶 Cloud 存储

提问于
浏览
0

我正在测试下面的一段代码,看看我是否可以将图像上传到Google的 Cloud 存储中的存储桶 .

这是我的app.yaml文件:

application: test-795  
version: 1
runtime: php
api_version: 1

handlers:

# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
  static_files: \1
  upload: .+\.(gif|png|jpg)$
  application_readable: true

- url: /(.+\.php).*
  script: \1  

- url: /upload
  script: fileimage.php

这是我的fileimage.php文件,用于将图像文件上传到存储桶:

<?php

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

 $options = [ 'gs_bucket_name' => 'test-795.appspot.com' ];
 $upload_url = CloudStorageTools::createUploadUrl('/upload', $options);

   if(isset($_POST['do-upload']) AND $_POST['do-upload'] === "yes"){

   $yesupload = $_POST['do-upload'];
   preg_match("/yes/", "".$yesupload."");

   $filename = $_FILES['testupload']['name'];

   $gs_name = $_FILES['testupload']['tmp_name'];
   move_uploaded_file($gs_name, 'gs://test-795.appspot.com/'.$filename.'');
?>

<div class="contentArea">
<?php
echo "<p class=\"SomeSpaceDude\">Hey file is uploaded, Yes! ".$yesupload." !</p>";
echo "<p class=\"SomeSpaceDude\">This is the file name: ".$filename."</p>";
}

echo"</div>";
?>

<form class="SomeSpaceDude" action="<?php echo $upload_url?>" enctype="multipart/form-data" method="post">
<p>Files to upload: </p> <br>
 <input type="hidden" name="do-upload" value="yes">
 <input class="SomeSpaceDude topcoat-button" type="file" name="testupload" >
 <input class="SomeSpaceDude topcoat-button" type="submit" value="Upload">
</form>
</div>

当我使用localhost:8080运行文件时,我收到消息 Hey file is uploaded, Yes! yes ! 我的网址变得像:localhost:8080 / _ah / upload / ahVkZXZ-Y ...在google存储区检查我的存储桶,其名称为test-795,没有任何内容已上传 .

我也部署了它,当我按下上传按钮后测试它时,我得到一个空页面和网址链接看起来像:test-795.appspot.com/_ah/upload/AMmfu6 ...检查我的桶什么都没有再次当然 . 知道我在这里缺少什么吗?

2 回答

  • 1

    当您在开发服务器中运行时,文件不会上传到真正的 Cloud 存储服务,只会上传到本地模拟 .

    在 生产环境 服务器上,检查日志以查看上载可能发生的情况 .

  • 3

    bove代码工作得很好......我终于发现问题是,我应该在上面的代码test-795中使用,而不是test-795.appspot.com

相关问题