Laravel 中是否存在 CreateContainerIfNotExists 方法?

问题描述 投票:0回答:1

我想将我的图像上传到 BLOB 存储中,但我没有找到像 CreateContainerIfNotExists 这样的方法,但 ChatGPT 做了这个“拐杖”

private function uploadToAzureBlobStorage($file, $imageName)
    {
        $blobRestProxy = BlobRestProxy::createBlobService("AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;");

        $containerName = 'block-image';

        // create container if not exists
        $listContainersOptions = new ListContainersOptions();
        $containers = $blobRestProxy->listContainers($listContainersOptions);

        $containerExists = false;
        foreach ($containers->getContainers() as $container) {
            if ($container->getName() === $containerName) {
                $containerExists = true;
                break;
            }
        }

        if (!$containerExists) {
            $createContainerOptions = new CreateContainerOptions();
            $createContainerOptions->setPublicAccess('container'); 
            $blobRestProxy->createContainer($containerName, $createContainerOptions);
        }

        // upload image
        $content = fopen($file->getPathname(), 'r');
        $options = new CreateBlockBlobOptions();
        $options->setContentType($file->getMimeType());

        $blobRestProxy->createBlockBlob($containerName, $imageName, $content, $options);
        $blobUrl = $blobRestProxy->getBlobUrl($containerName, $imageName);
        return $blobUrl;
    }

Laravel 中是否存在 CreateContainerIfNotExists 方法?或者我需要做这样的事情?

laravel azure azure-blob-storage
1个回答
0
投票

Laravel 中是否存在 CreateContainerIfNotExists 方法?

不,PHP 的

microsoft/azure-storage-blob
包中不存在 CreateConatinerIfNotexixts 方法。

这是使用 Laravel 将图像从本地上传到 Azure Blob 存储的代码。

代码:

public function uploadImage(Request $request)
    {
        $this->validate($request, [
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        // Assuming 'image' is the name attribute of the file input
        $file = $request->file('image');
        $connectionString = "xxxxxxxx";  // replace your storage connection string
        $containerName = 'block-image';
        $imageName = time() . '.' . $file->getClientOriginalExtension();

        $blobRestProxy = BlobRestProxy::createBlobService($connectionString);

        // Check if container exists, and create it if not
        $listContainersOptions = new ListContainersOptions();
        $containers = $blobRestProxy->listContainers($listContainersOptions);

        $containerExists = false;
        foreach ($containers->getContainers() as $container) {
            if ($container->getName() === $containerName) {
                $containerExists = true;
                break;
            }
        }

        if (!$containerExists) {
            $createContainerOptions = new CreateContainerOptions();
            $createContainerOptions->setPublicAccess('container');
            
            try {
                $blobRestProxy->createContainer($containerName, $createContainerOptions);
            } catch (StorageServiceException $e) {
                return response()->json(['error' => $e->getMessage()], 500);
            }
        }

        // Upload image
        $content = fopen($file->getPathname(), 'r');
        $options = new CreateBlockBlobOptions();
        $options->setContentType($file->getMimeType());

        try {
            $blobRestProxy->createBlockBlob($containerName, $imageName, $content, $options);
            $blobUrl = $blobRestProxy->getBlobUrl($containerName, $imageName);
            return response()->json(['message' => 'Image uploaded successfully', 'blob_url' => $blobUrl]);
        } catch (StorageServiceException $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }

Web.php:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AzureBlobStorageController;

Route::get('/upload-form', function () {
    return view('upload-form');
});

Route::post('/upload-image', [AzureBlobStorageController::class, 'uploadImage']);

上传-form.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Upload Form</title>
</head>
<body>
    <h1>Upload an Image</h1>

    <form action="/upload-image" method="post" enctype="multipart/form-data">
        @csrf
        <label for="image">Choose an image:</label>
        <input type="file" name="image" id="image">
        <button type="submit">Upload Image</button>
    </form>
</body>
</html>

输出: enter image description here

enter image description here

上传后,您将收到以下图片消息:

enter image description here

上面的代码已执行并成功将图像上传到azure blob存储,并设置了容器的访问级别。

传送门:

enter image description here

参考:

azure-storage-php/azure-storage-blob at master · Azure/azure-storage-php · GitHub

© www.soinside.com 2019 - 2024. All rights reserved.