Shoaib Hassan
Shoaib Hassan

Reputation: 43

How to send the extension of the file too via code in laravel to the gcloud app storage bucket?

Am storing my pictures in gcloud app storage bucket via coding in laravel it's working.

But I want to save them with the same name as I saved them in the DB with their extension, so I can retrieve them easily.

The image is saving in the GCS bucket with the name automatically created by them and without extension.

Here's my code:

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();

}

Upvotes: 0

Views: 91

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 75910

You can specify 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
    );

Upvotes: 2

Related Questions