[英]how to upload file to subfolder in bucket in google cloud storage using php
i want to upload file to subfolder in bucket how can i do that can anyone please help me to do that 我想将文件上传到存储桶中的子文件夹,我该怎么办,有人可以帮助我做到这一点吗?
am able to uplaod to bucket but unable to upload to subfolder in bucket 能够升级到存储桶,但无法上传到存储桶中的子文件夹
my bucket name is fb-uploads 我的存储桶名称是fb-uploads
subfolder is china 子文件夹是中国
below is my code 下面是我的代码
<?php
require 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient([ 'keyFilePath' => 'facebooth_bucket.json']);
$bucketName = 'fb-uploads';
$uploadDocument = isset($_FILES['uploaded_file']) ? $_FILES['uploaded_file'] : '';
$uploaddir = "uploads/";
if (isset($uploadDocument['name']) && !empty($uploadDocument['name'])) {
$uploadFileName = str_replace(" ","_",basename($uploadDocument['name']));
$ext = pathinfo($uploadFileName, PATHINFO_EXTENSION);
$fileName = str_replace("." . $ext, "", $uploadFileName);
$uploadFileName = $fileName . "_" . date('his') . '.' . $ext;
$uploadfile = $uploaddir . $uploadFileName;
// die($uploadfile);
if (move_uploaded_file($uploadDocument['tmp_name'], $uploadfile)) {
if(file_exists($uploadfile)) {
try {
$objectName = $uploadFileName;
$file = fopen($uploadfile, 'r');
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload('/china/'.$uploadfile, [
'name' => $objectName
]);
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($uploadFileName), $bucketName, $objectName);
// require_once __DIR__ . "/google_storage/vendor/autoload.php";
// $storage = new StorageClient([
// 'keyFilePath' => '/var/www/html/crewbies/upload_testing/google_storage/key/facebooth_bucket.json'
// ]);
// $bucket = $storage->bucket('fb-uploads');
// $bucket->upload(
// fopen('/china/$uploadfile', 'r')
// );
}
catch(Exception $e) {
$err = $e->getMessage();
print_r($err);
die('heee');
}
}
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="uploaded_file" />
<input type="submit"/>
</form>
</body>
</html>
i want to upload file to subfolder in bucket how can i do that can anyone please help me to do that 我想将文件上传到存储桶中的子文件夹,我该怎么办,有人可以帮助我做到这一点吗?
Documentation how subfolders work. 记录子文件夹的工作方式。
The object gs://your-bucket/abc/def.txt is just an object that happens to have "/" characters in its name. 对象gs://your-bucket/abc/def.txt只是名称中恰好带有“ /”字符的对象。
There is no "abc" directory; 没有“ abc”目录; just a single object with the given name.
只是一个具有给定名称的对象。
I understand that your object looks like that: 我了解您的对象看起来像这样:
"/china/file-name.xxx"
but should be 但应该是
"china/file-name.xxx"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.