使用 Google Cloud Storage PHP API 设置 CORS 配置不起作用



我正在使用PHP API设置Google Cloud Storage bucket CORS配置,但它似乎不起作用

我阅读了以下文件中给出的文件:https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.96.0/storage/bucket

这是我的Laravel源代码:

use GoogleCloudCoreServiceBuilder;
...
$projectId = 'myProjectId';
$bucketName = 'myBucketName';
$gcloud = new ServiceBuilder([
'keyFilePath' => 'resources/google-credentials.json',
'projectId' => $projectId
]);
$storage = $gcloud->storage();
$bucket = $storage->bucket($bucketName);
//change bucket configuration
$result = $bucket->update([
'cors' => [
'maxAgeSeconds' => 3600,
'method' => [
"GET","HEAD"
],
"origin" => [
"*"
],
"responseHeader" => [
"Content-Type"
]
]
]);
//print nothing and bucket doesn't changed
dd($bucket->info()['cors']);

执行此代码后,存储桶 CORS 配置不会更改 (我的老板不希望我使用 gsutil shell 命令来处理这个问题)

你非常接近!CORS 接受列表,因此您只需稍作修改:

$result = $bucket->update([
'cors' => [
[
'maxAgeSeconds' => 3600,
'method' => [
"GET","HEAD"
],
"origin" => [
"*"
],
"responseHeader" => [
"Content-Type"
]
]   
]
]);

让我知道它是否对:)有帮助。

我唯一需要更改的是当我在 laravel 中配置磁盘时,在配置文件/文件系统中使用以下代码.php在为 google 添加磁盘时:

'google' => [
'driver' => 's3',
'key' => 'xxx',
'secret' => 'xxx',
'bucket' => 'qrnotesfiles',
'base_url'=>'https://storage.googleapis.com'
]

下面是代码示例 fist 从请求中获取文件内容:

$file = $request->file('avatar')

第二次将其保存到存储中:

Storage::disk('google')->put('avatars/' , $file);

最新更新