php - 上传到 Google Cloud Bucket - 凭据无效
问题描述
我正在尝试将文件上传到我的 Google Cloud Bucket,但是我不确定如何包含凭据。我有一个包含凭据的 .json 文件,并且可以创建一个 ServiceBuilder,但我不知道如何使用它来上传文件(我一直在使用这个:https ://github.com/GoogleCloudPlatform/google -cloud-php#google-cloud-storage-ga)
以下返回 401:Invalid Credentials:
<?php
require 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Core\ServiceBuilder;
// Authenticate using a keyfile path
$cloud = new ServiceBuilder([
'keyFilePath' => 'keyfile.json'
]);
$storage = new StorageClient([
'projectId' => 'storage-123456'
]);
$bucket = $storage->bucket('storage-123456');
// Upload a file to the bucket.
$bucket->upload(
fopen('data/file.txt', 'r')
);
// Using Predefined ACLs to manage object permissions, you may
// upload a file and give read access to anyone with the URL.
$bucket->upload(
fopen('data/file.txt', 'r'),
[
'predefinedAcl' => 'publicRead'
]
);
// Download and store an object from the bucket locally.
$object = $bucket->object('file_backup.txt');
$object->downloadToFile('data/file_backup.txt');
?>
解决方案
啊,我是厚。
不需要服务构建器:
$storage = new StorageClient([
'keyFilePath' => 'keyfile.json',
'projectId' => 'storage-123456'
]);
推荐阅读
- bash - 脚本中没有输出为 stdout 和 stderr
- node.js - 如何将图像从前端(reactjs)传递到后端(nodejs)并将其上传到firebase存储?
- html - react-perfect-scrollbar - 不会为 div 持有手风琴更新
- typescript - Angular TSLint:即使出现错误也继续下一步
- flutter - Flutter Chopper 库缓存 http 响应
- python - 如何在单行中编写此 python 条件
- sql-server - SSRS 2017 报告通过 Intranet 连接加载缓慢,通过 Web 链接通过 Internet 连接立即加载
- date - 在 ElasticSearch 中按日期和按天写入次数排序(二次排序)
- ios - 多行截断中间不能快速工作
- sql - 两个事务之间的写冲突