PHP运用微软云进行图片上传
不做过多赘述,直接上php方法
不做过多赘述,直接上php方法
class Azure extends \think\Controller
{
//直接上传
public function uploadazure1()
{
$AccountName = "******";
$AccountKey = "******";
$connectionString = "******";
$containerName = "******";
try {
$tmpName = $_FILES['uploadkey']['tmp_name'];
$fileName = $_FILES['uploadkey']['name'];
$type = pathinfo($fileName, PATHINFO_EXTENSION);
$content = fopen($tmpName, "r");
$blob_name = md5($fileName) . "_" . time() . '.' . $type;
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
/* // Create container options object.
$createContainerOptions = new CreateContainerOptions();
// Set public access policy. Possible values are
// PublicAccessType::CONTAINER_AND_BLOBS and PublicAccessType::BLOBS_ONLY.
// CONTAINER_AND_BLOBS: full public read access for container and blob data.
// BLOBS_ONLY: public read access for blobs. Container data not available.
// If this value is not specified, container data is private to the account owner.
$createContainerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS);
// Create container.
$blobRestProxy->createContainer($containerName, $createContainerOptions);*/
//Upload blob
$blobRestProxy->createBlockBlob($containerName, $blob_name, $content);
$filePath = "https://$AccountName.blob.core.windows.net/$containerName/$blob_name";
return $filePath;
} catch (ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
$msg = $code . ": " . $error_message . "
";
return false;
}
}
//base64上传1
public function uploadAzure2($base64_dataUrl,$userid)
{
$AccountName = "******";
$AccountKey = "******";
$connectionString = "******";
$containerName = "******";
try {
//匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_dataUrl, $result)) {
$type = $result[2];
$blob_name = $userid . '_' . time() . ".{$type}";
$content = base64_decode(str_replace($result[1], '', $base64_dataUrl));
}else{
return false;
}
// $content = $base64_dataUrl;
// $path = base64_image_content_azure($base64_dataUrl,$userid);
// $blob_name = $path;
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
/* // Create container options object.
$createContainerOptions = new CreateContainerOptions();
// Set public access policy. Possible values are
// PublicAccessType::CONTAINER_AND_BLOBS and PublicAccessType::BLOBS_ONLY.
// CONTAINER_AND_BLOBS: full public read access for container and blob data.
// BLOBS_ONLY: public read access for blobs. Container data not available.
// If this value is not specified, container data is private to the account owner.
$createContainerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS);
// Create container.
$blobRestProxy->createContainer($containerName, $createContainerOptions);*/
//Upload blob
$blobRestProxy->createBlockBlob($containerName, $blob_name, $content);
$filePath = "https://$AccountName.blob.core.windows.net/$containerName/$blob_name";
return $filePath;
} catch (ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
$msg = $code . ": " . $error_message . "
";
return false;
}
}
public function delete($blob_name) {
$AccountName = "f9fcdbf3fa3b411e9a12932";
$AccountKey = "92y1qFbJu5eUQpVe3m8d+CoT2GOCaIjTSCkVF5ECAhOlFYuIRopmdXfC5qtDP5CCDFXNn1G7nPX/8Jq7bJznMw==";
$connectionString = "DefaultEndpointsProtocol=https;AccountName=$AccountName;AccountKey=$AccountKey";
$containerName = "mycontainer";
try {
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
// $blob_name = $_POST['blob_name'];
$blobRestProxy->deleteBlob($containerName, $blob_name);
// return $this->jsonData(1,'删除成功');
} catch (ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
$msg = $code . ": " . $error_message . "
";
// return $this->jsonData(0,'删除失败:'.$msg);
}
}
}
2.关于引用的microsoft插件可以去我的上传资源下载