首先登陆自己的腾讯云账号
在对象存储中创建自己的存储空间
同时生成好自己的secretid和secretkey
来到对象存储SDK文档中
地址:https://cloud.tencent.com/document/product/436/12266
按照步骤先部署配置
建立方法后使用
$img = $request->post();
$img_herf=public_path()."/tmp/".$img['name'];
$img_herf = str_replace('\\','/',$img_herf);
//这里需要把你你接收到的文件路径拼装成绝对路径
$secretId = "你的API 密钥"; //"云 API 密钥 SecretId";
$secretKey = "你的API 密钥 SecretKey"; //"云 API 密钥 SecretKey";
$region = "你的存储桶(例如:ap-nanjing)"; //设置一个默认的存储桶地域
$cosClient = new Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey
)
));
try {
$bucket = "你的桶名称"; //存储桶名称 格式:BucketName-APPID
$key = $img['name']; //此处的 key 为对象键,对象键是对象在存储桶中的唯一标识
$srcPath = $img_herf;//本地文件绝对路径
$file = fopen($srcPath, "rb");
if ($file) {
$result = $cosClient->putObject(array(
'Bucket' => $bucket,
'Key' => $key,
'Body' => $file));
$img = "https://".$result['Location'];
print_r($img);
}
} catch (\Exception $e) {
echo "$e\n";
}
这样就会返回出你的图片保存地址,可以在页面上直接展示