![](/img/trans.png)
[英]Upload a file to Google Cloud Storage Bucket with PHP and Ajax
[英]how to upload file to subfolder in bucket in google cloud storage using php
我想将文件上传到存储桶中的子文件夹,我该怎么办,有人可以帮助我做到这一点吗?
能够升级到存储桶,但无法上传到存储桶中的子文件夹
我的存储桶名称是fb-uploads
子文件夹是中国
下面是我的代码
<?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>
我想将文件上传到存储桶中的子文件夹,我该怎么办,有人可以帮助我做到这一点吗?
对象gs://your-bucket/abc/def.txt只是名称中恰好带有“ /”字符的对象。
没有“ abc”目录; 只是一个具有给定名称的对象。
我了解您的对象看起来像这样:
"/china/file-name.xxx"
但应该是
"china/file-name.xxx"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.