【问题标题】:How to upload an image to Azure storage through PHP?如何通过 PHP 将图像上传到 Azure 存储?
【发布时间】:2019-05-15 04:40:18
【问题描述】:

我正在尝试通过我用 PHP 构建的 API 将图像上传到我的 blob 存储。

目前我有这个,base64 字符串正在通过 JSON Post 发送。

//The base 64 string
$displayPictureBase64 = $this->ValidateParameter('DisplayPicture', $this->param, STRING);
//Decode it to byte array.
$displayPicture[] = base64_decode($displayPictureBase64);
//Name of the blob
$blobName = "MyBlobName";

//New BlobStorage class.
$blob = new BlobStorage;
$blob->AddBlob('user-display-pictures', $blobName, $displayPicture);

这会调用函数 AddBlob...

public function AddBlob($containerName, $fileName, $fileToUpload)
{
//Upload blob
$this->blobClient->createBlockBlob($containerName, $fileName, $fileToUpload);
}

(PS。我有 blobClient 的凭据,只是没有在此处包含它以节省不必要的空间。)

我遇到的问题是函数 blobClient->createBlockBlob 采用这些参数...

 * @param string                          $container The name of the container.
 * @param string                          $blob      The name of the blob.
 * @param string|resource|StreamInterface $content   The content of the blob.

所以我遇到的问题是我发送的第三个参数是数组类型,但根据这个它应该是字符串。 这是我得到的 PHP 错误...

PHP 致命错误:未捕获的 InvalidArgumentException:无效资源类型:D:\home\vendor\guzzlehttp\psr7\src\functions.php:116 中的数组

如何将图像作为字符串上传到 blob 存储?此处的docs 仅显示如何上传文本文件,而不是图像文件。谢谢!

【问题讨论】:

    标签: php azure blob storage


    【解决方案1】:

    我通过以下操作修复了它...

    //The base 64 string
    $displayPictureBase64 = $this->ValidateParameter('DisplayPicture', $this->param, STRING);
    
    //Convert the file to stream
    $fileToUpload = fopen('data:image/jpeg;base64,' . $displayPictureBase64,'r');
    //Name of the blob
    $blobName = "MyBlobName";
    
    //New BlobStorage class.
    $blob = new BlobStorage;
    $blob->AddBlob('user-display-pictures', $blobName, $fileToUpload);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 2022-11-03
      • 2018-12-17
      • 1970-01-01
      • 2017-03-11
      • 2020-09-08
      • 2021-07-22
      相关资源
      最近更新 更多