Php 上载到Google云存储桶-凭据无效
我正在尝试将一个文件上传到我的Google Cloud Bucket,但是我不确定如何包含凭据。我有一个包含凭据的.json文件,可以创建一个ServiceBuilder,但我不知道如何使用它来上传文件(我一直在使用它:) 以下内容返回401:无效凭据:Php 上载到Google云存储桶-凭据无效,php,google-cloud-platform,google-cloud-storage,Php,Google Cloud Platform,Google Cloud Storage,我正在尝试将一个文件上传到我的Google Cloud Bucket,但是我不确定如何包含凭据。我有一个包含凭据的.json文件,可以创建一个ServiceBuilder,但我不知道如何使用它来上传文件(我一直在使用它:) 以下内容返回401:无效凭据: <?php require 'vendor/autoload.php'; use Google\Cloud\Storage\StorageClient; use Google\Cloud\Core\ServiceBuild
<?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'
]);