php - 如何通过PHP将图像上传到Azure存储?

标签 php azure blob storage

我正在尝试通过使用 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 Fatal error: Uncaught InvalidArgumentException: Invalid resource type: array in D:\home\vendor\guzzlehttp\psr7\src\functions.php:116

如何将图像作为字符串上传到 blob 存储? docs这里只展示如何上传文本文件,不展示图像文件。谢谢!

最佳答案

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

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

关于php - 如何通过PHP将图像上传到Azure存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53770747/

相关文章:

php - Zend\File\Transfer\Adapter\Http on receive : error "File was not found" with jQuery File Upload

javascript - setCustomValidity Bootstrap 保持错误弹出

azure - 在 kubernetes 集群上部署 Cassandra

使用 appsettings.json 时 Azure 函数无法初始化 Serilog Sink

php - 仅显示数据库中的第一个条目

java.lang.AbstractMethodError : com. mysql.jdbc.PreparedStatement.setBlob(ILjava/io/InputStream;)V

php - 从服务器和数据库中删除文件 (PHP)

php - 如何将每个结果添加到 MySQL 中的表中

Azure 应用服务 Application Insight 显示 API 等待很长时间

javascript - 打开PDF是空白的