简体   繁体   English

Google云端存储-无效的存储桶名称(PHP)

[英]Google Cloud Storage - Invalid bucket name (PHP)

I tried to upload a file on root in my Google cloud Storage by doing this: 我尝试通过执行以下操作在Google云端存储的根目录上载文件:

$bucketName = "bucket-name.appspot.com";
        $objectName = $client_id . '_' . rand(0, 200) . '_' . $type . '.pdf';
        $storage = new StorageClient();
        $bucket = $storage->bucket($bucketName);
        $object = $bucket->upload($datas, [
            'name' => $objectName
        ]);

This worked and i successfully upload a file($datas) . 这有效,我成功上传了一个file($datas)


Now i created a folder in my root storage and called it "contracts" . 现在,我在根存储中创建了一个文件夹,并将其称为"contracts" So my $bucketName is updated to include the folder like this: 因此,我的$bucketName已更新为包括以下文件夹:

$bucketName = "bucket-name.appspot.com/contracts";
        $objectName = $client_id . '_' . rand(0, 200) . '_' . $type . '.pdf';
        $storage = new StorageClient();
        $bucket = $storage->bucket($bucketName);
        $object = $bucket->upload($datas, [
            'name' => $objectName
        ]);

As you can see this is now updated to: $bucketName = "bucket-name.appspot.com/contracts"; 如您所见,现在已更新为: $bucketName = "bucket-name.appspot.com/contracts"; . But this gives me this error now: 但这现在给我这个错误:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "Invalid bucket name: 'bucket-name.appspot.com/contracts'"
   }
  ],
  "code": 400,
  "message": "Invalid bucket name: 'bucket-name.appspot.com/contracts'"
 }
}

Why is that? 这是为什么?

See: 看到:

You should add /contracts/ to $objectName . 您应该将/contracts/添加到$objectName

$bucketName = "bucket-name.appspot.com";
$objectName = '/contracts/' . $client_id . '_' . rand(0, 200) . '_' . $type . '.pdf';
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($datas, [
    'name' => $objectName
]);

Withing Google Cloud Storage, there are only buckets and objects . 使用Google Cloud Storage时,只有bucketsobjects The notion of folders is only an illusion . 文件夹的概念只是一种幻想 For example, a file that you wish to access as gs://yourbucket/yourfolder/yourfile is bucket yourbucket and file (object) yourfolder/yourfile . 例如,您希望以gs://yourbucket/yourfolder/yourfile yourbucket文件是bucket yourbucket和文件(对象) yourfolder/yourfile

There is a dedicated article on this story at How Subdirectories work . 关于子目录的工作原理,有一篇专门的文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在谷歌云存储桶中运行 PHP - Run PHP in Google Cloud Storage Bucket Google Cloud Storage 对存储桶中的对象进行分页 (PHP) - Google Cloud Storage paginate objects in a bucket (PHP) 是否可以仅使用 Authorization Bearer Token 和 Bucket 名称将文件上传到谷歌云存储? (PHP代码) - Is it possible to upload files to google cloud storage with just Authorization Bearer Token and Bucket name? (php code) 使用PHP和Ajax将文件上传到Google Cloud Storage Bucket - Upload a file to Google Cloud Storage Bucket with PHP and Ajax 使用php在存储桶谷歌云存储中创建文件夹 - Creating folder in bucket google cloud storage using php 使用PHP将文件上传到Google Cloud Storage Bucket - Upload files to Google Cloud Storage Bucket using PHP 如何使用php将文件上传到Google云存储中存储桶中的子文件夹 - how to upload file to subfolder in bucket in google cloud storage using php 如何仅使用 OAuth 2.0 不记名令牌和存储桶名称在谷歌云存储中生成文件的签名 URL? (PHP代码) - How to generate a Signed URL of file in google cloud storage with just OAuth 2.0 bearer token and bucket name? (php code) PHP Google云端存储 - PHP Google Cloud Storage 图像上传到谷歌桶和云存储 - image upload to google bucket ay cloud storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM