【问题标题】:upload image file to google cloud storage将图像文件上传到谷歌云存储
【发布时间】:2015-02-28 22:13:23
【问题描述】:

我试图按照谷歌云存储文档here 将图像文件上传到谷歌云存储,但没有成功。

我在我的谷歌云存储中创建的存储桶名为:test_app-111

这是我的 app.yaml 文件:

application: test_app-111  
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: /img.php
  script: img.php 

这是我的名为 img.php 的 php 文件:

<?php

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

$options = [ 'gs_bucket_name' => 'https://storage.googleapis.com/test_app-111.appspot.com' ];
$upload_url = CloudStorageTools::createUploadUrl('/upload_handler.php', $options);

?>


<form action="<?php echo $upload_url?>" enctype="multipart/form-data" method="post">
    Files to upload: <br>
   <input type="file" name="uploaded_files" size="40">
   <input type="submit" value="Send">
</form>

知道我错过了什么吗?在文档中它说“使用 CloudStorageTools::createUploadUrl() 方法创建应用程序特定的上传 URL”。我必须自己创建一个上传 URL 吗?有人知道如何继续前进吗?

【问题讨论】:

  • 你得到什么错误?
  • 我使用我的 SDK 运行它。我在localhost:8080/img.php 打开 chrome 然后我选择了一个图片按上传...什么都没有出现,得到一个没有任何错误的空白页面。在谷歌云存储中检查我的存储桶时,里面没有图像。
  • 您的存储桶名称应该类似于:https://storage.googleapis.com/test_app-111.appspot.com 删除 gs_bucket_name 参数,它应该会上传到您的默认存储桶。
  • 确保您有权写入该存储桶:cloud.google.com/appengine/docs/python/googlestorage/… 另外,尝试从那里部署和上传。在本地尝试时可能是权限问题。
  • 我更改了存储桶名称,正如您在上面的代码中看到的那样,但仍然没有。如果我去我的谷歌云存储并查看我的存储桶,我可以手动上传文件或从控制台删除

标签: php google-app-engine google-cloud-storage image-upload


【解决方案1】:

gs_bucket_name 应该设置为您的存储桶名称,而不是完整的 url,即

$options = [ 'gs_bucket_name' => 'test_app-111.appspot.com' ];

您也可以使用CloudStorageTools::getDefaultGoogleStorageBucketName() 来检索默认存储桶名称,而不是硬编码。

【讨论】:

    猜你喜欢
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2022-08-20
    • 1970-01-01
    相关资源
    最近更新 更多