php - 无法通过 Google Compute Engine 使用 PHP 在 Google Cloud Storage 上上传文件
问题描述
我正在尝试将图像文件上传到 GCS 中的存储桶,只在我的页面上插入了一个文件输入和一个按钮进行一些测试,当我单击该按钮时,它什么也没做,没有错误,什么也没有。我在 Google Cloud Platform Compute Engine 中使用 Ubuntu VM。实际上我不能用谷歌云存储客户端做任何事情,不能上传对象,列出对象,列出存储桶,我已经把它上传到我的网站而且它不起作用,首先在 localhost 上尝试但它给了我这个错误:cURL 错误 60:SSL 证书问题:无法获取本地颁发者证书,这就是我将其上传到 GCP 上的 VM 的原因。
<?php
require_once __DIR__.'/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
class googleStorage{
private $projectId;
private $storage;
public function __construct() {
//putenv("GOOGLE_APPLICATION_CREDENTIALS=/var/www/html/vendor/google/Cloud/credentials.json");
$this->projectId = 'my_project_id';
$this->storage = new StorageClient([
'projectId' => $this->projectId
]);
$this->storage->registerStreamWrapper();
}
function uploadObject($bucketName, $objectName, $source) {
$file = fopen($source, 'r');
$bucket = $this->storage->bucket($bucketName);
$object = $bucket->upload($file, [
'name' => $objectName
]);
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
}
public function listBuckets() {
$buckets = $this->storage->buckets();
foreach ($buckets as $bucket) {
echo $bucket->name() . PHP_EOL;
}
}
function listObjects($bucketName) {
$bucket = $this->storage->bucket($bucketName);
foreach ($bucket->objects() as $object) {
printf('Object: %s' . '<br>', $object->name());
}
}
function getImageUrl($bucketName, $objectName) {
return 'https://storage.cloud.google.com/'.$bucketName.'/'.$objectName;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap Select-->
<link rel="stylesheet" href="vendor/bootstrap-select/css/bootstrap-select.min.css">
<title>Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="foto" class="form-control">
<button type="submit" name="upload" class="form-control">Upload</button>
</form>
<?php
include "googleStorage.php";
$bucket = "my_bucket";
$storage = new googleStorage();
$storage->listBuckets();
$storage->listObjects($bucket);
$imageUrl = $storage->getImageUrl($bucket, 'stitch.jpg');
if(isset($_POST['upload'])){
$storage->uploadObject($bucket, $_FILES['foto']['name'], $_FILES['foto']['tmp_name']);
}
?>
</body>
</html>
解决方案
要使 Compute Engine vm 实例能够访问 Cloud Storage,请执行以下步骤:
- 转到计算引擎
- 单击您的虚拟机实例
- 检查最底部的Cloud API 访问范围。
注意:如果设置为Allow default access,则必须通过以下步骤进行更改:
停止您的虚拟机并单击编辑
转到Cloud API 访问范围
选择以下选项:
A.允许完全访问所有 Cloud API,以访问所有 Cloud API,包括 Cloud Storage。
B.为每个 API 设置访问权限,只有选定的 Cloud API 才有访问权限。
保存并启动/恢复您的虚拟机。
推荐阅读
- css - 动画 - 线应该跟随
- c# - 现场实例化抽象类
- java - Spring Boot 应用程序无法重新连接到 Redis 并找到 Consumer 组
- java - 如何验证 PoolingHttpClientConnectionManager 是否应用于球衣客户端
- ruby-on-rails - Ruby on Rails 在现有资源上返回 404
- javascript - JavaScript:如何在不更改源对象的情况下修改复制的对象?
- swiftui - 我想在 SwiftUI 中向 TextField 添加 $ 符号
- python-3.x - 运行由另一个服务通过 requests.post() 调用的 API 服务的最佳 gunicorn 配置是什么?我应该使用 gevents 还是同步工作人员?
- python - 从我无法继承的另一个类调用方法
- jenkins - 错误:执行失败;`/bin/launchctl bootstrap gui/501 /Users/ec2-user/Library/LaunchAgents/homebrew.mxcl.jenkins-lts.plist` 以 37 退出