如何通过 laravel 中的代码将文件的扩展名也发送到 gcloud 应用程序存储桶?
How to send the extension of the file too via code in laravel to the gcloud app storage bucket?
我正在通过 laravel 中的编码将我的照片存储在 gcloud 应用程序存储桶中。它正在工作。
但我想用与我在数据库中保存它们的扩展名相同的名称保存它们,这样我就可以轻松检索它们。
图像以他们自动创建的名称保存在 GCS 存储桶中,没有扩展名。
这是我的代码:
public function store(Request $request)
{
if ($request->hasFile('image')) {
$imageName = time().'.'.$request->image->extension();
$photo = $request->image->storeAs('ufurnitures-img', $imageName, 'public');
}
// Save the data into GCS
$extension = $request->image->getClientOriginalExtension();
$storage = new StorageClient([
'projectId' => 'ufurnitures'
]);
$bucket = $storage->bucket('ufurnitures-img');
$bucket->upload(
//fopen($request->image.'.'.$extension, 'r') //This one is not working
fopen($request->image, 'r') //This one is working
);
// Save the data into database
Product::create([
'name' => $request->name,
'price' => $request->price,
'description' => $request->description,
'image' => $photo
]);
$request->session()->flash('msg','Your product has been added');
return redirect()->back();
}
您可以指定options when you upload your blob。
$options = [
'name' => '/path/to/file.extension',
];
$bucket->upload(
//fopen($request->image.'.'.$extension, 'r') //This one is not working
fopen($request->image, 'r'), //This one is working
$options
);
我正在通过 laravel 中的编码将我的照片存储在 gcloud 应用程序存储桶中。它正在工作。
但我想用与我在数据库中保存它们的扩展名相同的名称保存它们,这样我就可以轻松检索它们。
图像以他们自动创建的名称保存在 GCS 存储桶中,没有扩展名。
这是我的代码:
public function store(Request $request)
{
if ($request->hasFile('image')) {
$imageName = time().'.'.$request->image->extension();
$photo = $request->image->storeAs('ufurnitures-img', $imageName, 'public');
}
// Save the data into GCS
$extension = $request->image->getClientOriginalExtension();
$storage = new StorageClient([
'projectId' => 'ufurnitures'
]);
$bucket = $storage->bucket('ufurnitures-img');
$bucket->upload(
//fopen($request->image.'.'.$extension, 'r') //This one is not working
fopen($request->image, 'r') //This one is working
);
// Save the data into database
Product::create([
'name' => $request->name,
'price' => $request->price,
'description' => $request->description,
'image' => $photo
]);
$request->session()->flash('msg','Your product has been added');
return redirect()->back();
}
您可以指定options when you upload your blob。
$options = [
'name' => '/path/to/file.extension',
];
$bucket->upload(
//fopen($request->image.'.'.$extension, 'r') //This one is not working
fopen($request->image, 'r'), //This one is working
$options
);