php - 无法使用 MS-Azure Blob 存储中的文件路径上传图像
问题描述
我正在使用 Microsoft Azure 服务开发一个 Web 应用程序。我正在按照此链接将图像上传到我的 blob 存储,但文档中缺少一些会导致某些问题的点点滴滴。例如,我们如何定义变量 $fileToUpload 以及它与变量 $myfile 的关系如何?另外,如果我们想在应用程序运行时选择从我们的系统上传文件,我们应该在这两个变量中的哪个变量中拥有我的图像的绝对文件路径?到目前为止,我编写的上传图片的代码如下:
function imageupload(string $current_user){
$pt_id = $_POST['patientid'];
if (strcmp($pt_id,"")==0){
echo "Select PatientID to continue";
}else{
$accountexists = load($current_user, $pt_id);
if ($accountexists == 0){
$error = "Patient Id does not exist";
echo $error;
}else{
$img_path = $_POST['uploadI'];
if (strcmp($img_path, "")==0){
echo "Enter valid image path <br />";
}
else{
require_once 'vendor/autoload.php';
$connectionString = '****';
// Create blob client.
$blobClient = BlobRestProxy::createBlobService($connectionString);
# Create the BlobService that represents the Blob service for the storage account
$containerName = $current_user;
$num = rand(10,10000);
$num2 = (string) $num;
$fileToUpload = $pt_id."_".$num2;
# Upload file as a block blob
echo "Uploading image: ".PHP_EOL;
echo $img_path;
echo "<br />";
$content = fopen($img_path, "r");
//Upload blob
$blobClient->createBlockBlob($containerName, $fileToUpload, $content);
echo "Image uploaded successfully! <br />";
}
}
}
}
在这段代码中,我将图像的路径存储在变量 $img_path 中。这是我稍后用于显示图像的代码:
// Gets all the images from the hospital's container and filters out the images corresponding to a particular patient.
// For the patient, the image would be saved on cloud with name as: <patientid>_<some random number>
// Eg- patient id= pt1 , so image name could be- pt1_3682
function uploadedimage(string $current_user, string $HospitalId, string $PatientId){
$containerName = $HospitalId;
$url = "";
try{
// Get blob.
require_once 'vendor/autoload.php';
// use MicrosoftAzure\Storage\Common\ServicesBuilder;
$connectionString = '****';
$blobClient = BlobRestProxy::createBlobService($connectionString);
$blob_list = $blobClient->listBlobs($containerName);
$blobs = $blob_list->getBlobs();
$strlen = strlen($PatientId);
$array_user = array();
// echo "These are the blobs present in the container: ";
foreach($blobs as $blob)
{
$name = $blob->getName();
$namesub = substr($name,0, $strlen);
if (strcmp($namesub, $PatientId) == 0){
array_push($array_user, $blob);
}
}
}
catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
$url = "";
try {
foreach($array_user as $blob)
{
$name = $blob->getName();
$url = $blob->getUrl();
echo "<img src=\"".$url."\" alt=\"image post\"></br></br>";
}
}
catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
}
但我似乎无法在我指定的 html 中查看我的图像。我得到的只是默认文本“图片帖子”。因此,我得出结论,我的代码中缺少一些东西,因为图像没有被上传,但我似乎无法从 azure 教程中弄清楚这一点。
编辑:我检查了我的 blob 属性,它们都是 0 大小,这意味着它们没有正确上传。我还意识到有一些图像与我的 git 文件夹中的代码一起被推送到云端。因此,如果名称为abc.png的特定图像已经存在于与我在 Azure 上的代码相同的目录中,并且在从我的 Web 应用程序上传此图像时,如果在路径文本框中我仅指定abc.png,则该图像会得到以正确的大小上传到我的容器中,但是如果我的桌面上存在相同的图像,并且我为此图像(或 azure 上存在的 git 目录中不存在的其他图像)指定本地系统的完整文件路径为C :/桌面/abc.png,它仅作为 0 字节图像上传(本质上没有正确上传)。如果它无法从我的系统中获取图像并且只能获取其 git 目录中已经存在的图像,可能会出现什么问题?
解决方案
推荐阅读
- java - Vaadin 传单地图图层图标删除
- r - 从数据框中的循环中保存真/假 match() 数据
- c++ - 多组只擦除一个实例
- visual-studio-code - 如何防止 vscode 创建“.vscode/settings.json”等工作区文件?
- php - 如何调用数组中的字符串
- python - 我怎样才能更简化呢?
- apache - Apache 将 https 非 www 重定向到 https www (Ubuntu)
- ruby - Ruby - 处理布尔值、if/then 语句、驳斥/断言方法,我的代码是否过于基础?
- java - 从键为 ArrayList 的哈希表中获取值
,并且查找是字符串 - oauth-2.0 - web api 2 - 禁止 Oauth2 Bearer 令牌访问