我正在尝试通过使用 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/