jasonLiu2018

阿里云OSS php 自定义域名 绑定bucket

做OSS图片上传时,可以采用阿里云分配的oss域名,也可以使用自定义域名(绑定用户域名),使用阿里云域名太长,不美观,我使用的绑定用户域名。使用阿里云分配域名时图片文件可以正常上传,使用自定义绑定用户域名时发现报错.

"\"绑定用户域名\"bucket name is invalid"

 

错误大概意思是绑定的用户域名没有关联到bucket上,在查看阿里云oss文档时发现还需要使用CNAME,使用CNAME绑定到域名域名
image
OSS文档地址:https://help.aliyun.com/document_detail/32100.html?spm=a2c4g.11186623.6.1055.181165d3LkhGyq
PHP上传代码处理逻辑

public function upload()
    {

        // 获取上传文件表单字段名
        $fileKey = array_keys(request()->file());

        //获取表单上传文件
        $file = request()->file($fileKey[\'0\']);
        $resResult = Image::open($file);
       
        try {   
            $config = Config::get(\'aliyun_oss\'); //获取Oss的配置
           
            //实例化对象 将配置传入
            $ossClient = new OssClient($config[\'KeyId\'], $config[\'KeySecret\'], $config[\'Endpoint\'],true);
            //这里是有sha1加密 生成文件名 之后连接上后缀
            $fileName = sha1(date(\'YmdHis\', time()) . uniqid()) . \'.\' . $resResult->type();
            
            //执行阿里云上传
            $result = $ossClient->uploadFile($config[\'Bucket\'], $config[\'path\'] . $fileName, $file->getInfo()[\'tmp_name\']);
           
            if ($result) {
                $result[\'code\'] = 1;
                $result[\'info\'] = \'图片上传成功!\';

                $result[\'url\'] = $result[\'oss-request-url\'];
                return $result;
            }
        } catch (OssException $e) {
            return $e->getMessage();
        }
}

 

image
需要注意,在实例化OSS对象时后面需要添加为true,这样就可以使用自定义域名了

原文地址: https://yq.aliyun.com/articles/750221

 

分类:

技术点:

相关文章:

  • 2021-12-15
  • 2021-11-22
  • 2021-09-27
  • 2021-11-15
  • 2021-11-16
  • 2021-12-28
  • 2021-10-14
  • 2021-12-15
猜你喜欢
  • 2021-11-11
  • 2021-09-07
  • 2021-11-12
  • 2021-12-23
  • 2021-08-15
  • 2021-10-14
  • 2021-11-17
相关资源
相似解决方案