【问题标题】:image upload to google bucket ay cloud storage图片上传到谷歌存储桶和云存储
【发布时间】:2015-03-01 12:19:54
【问题描述】:

我正在测试以下代码,看看我是否可以将图像上传到谷歌云存储的存储桶中。

这是我的 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 运行文件时,我收到消息 嘿,文件已上传,是的!是的! 我的网址变成这样:localhost:8080/_ah/upload/ahVkZXZ-Y... 检查我在谷歌存储中名为 test-795 的存储桶,但没有上传任何内容。

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

【问题讨论】:

    标签: php google-app-engine google-cloud-storage bucket


    【解决方案1】:

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

    在生产服务器上,检查日志以了解您的上传可能会发生什么。

    【讨论】:

    • 我也在尝试在本地使用 GCS。有没有办法查看模拟的存储桶?为什么我的上传在部署后 100% 正常工作时会在本地失败?
    【解决方案2】:

    上面的代码工作正常...我最终发现问题是,我应该在上面的代码 test-795 中的任何地方使用,而不是 test-795.appspot.com

    【讨论】:

      猜你喜欢
      • 2016-12-16
      • 2021-06-15
      • 2018-07-11
      • 2016-04-12
      • 2022-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      相关资源
      最近更新 更多