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 . "<br />";
            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 . "<br />";
            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 . "<br />";
           // return $this->jsonData(0,'删除失败:'.$msg);
        }
    }

  
}

2.关于引用的microsoft插件可以去我的上传资源下载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值