我尝试通过以下操作在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
]);
这有效,我成功上传了file($datas)
。
现在,我在根存储中创建了一个文件夹,并将其命名为"contracts"
。因此,我的$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
]);
如您所见,现在已更新为:$bucketName = "bucket-name.appspot.com/contracts";
。但这现在给我这个错误:
{
"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'"
}
}
那是为什么?
答案 0 :(得分:3)
请参阅:
您应将/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
]);
答案 1 :(得分:2)
有了Google Cloud Storage,只有buckets
和objects
。文件夹的概念只是一个错觉。例如,您希望以gs://yourbucket/yourfolder/yourfile
身份访问的文件是存储区yourbucket
和文件(对象)yourfolder/yourfile
。
How Subdirectories work上有一篇关于这个故事的专门文章。