$appid = 'id';
$bucket = '桶名';
$SecretId = 'id';
$SecretKey = 'key';
$path = '文件路径可自定义';
$region = '桶所在服务器缩写:ap-beijing';
$cosClient = new \Qcloud\Cos\Client(
array('region' => $region,
'credentials' => array(
'appId' => $appid,
'secretId' => $SecretId,
'secretKey' => $SecretKey
)
)
);
//获取bucket列表
//$result = $cosClient->listBuckets();
//print_r($result);
//return view( $this->_skin .'.index',compact('infos','infos_seo'));
//上传文件
// try {
// $result = $cosClient->upload(
// //bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
// $bucket= $bucket,
// //$key = '444.txt',//上传的文件名,可以不存在,下面的Body会给此文件写入内容
// //$body = '改变内容上传',//把变量内容写入到上面的$key文件中去并上传
// $key = '5.txt',//上传的文件名,可以不存在,下面的Body会给此文件写入内容
// $body = fopen('./hello.txt', 'rb'),//打开指定的文件把内容写入到上面的$key文件中去并上传
// $options = array(
// "ACL"=>'private',
// 'CacheControl' => 'private'));
// print_r($result);
// } catch (\Exception $e) {
// echo "$e\n";
// }
// 下载文件到本地
$result = $cosClient->getObject(array(
//bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
'Bucket' => $bucket,
'Key' => '5.txt',//服务器下载的文件
'SaveAs' => './444.txt'));//下载之后的文件路径及新命名
echo($result);
$bucket = '桶名';
$SecretId = 'id';
$SecretKey = 'key';
$path = '文件路径可自定义';
$region = '桶所在服务器缩写:ap-beijing';
$cosClient = new \Qcloud\Cos\Client(
array('region' => $region,
'credentials' => array(
'appId' => $appid,
'secretId' => $SecretId,
'secretKey' => $SecretKey
)
)
);
//获取bucket列表
//$result = $cosClient->listBuckets();
//print_r($result);
//return view( $this->_skin .'.index',compact('infos','infos_seo'));
//上传文件
// try {
// $result = $cosClient->upload(
// //bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
// $bucket= $bucket,
// //$key = '444.txt',//上传的文件名,可以不存在,下面的Body会给此文件写入内容
// //$body = '改变内容上传',//把变量内容写入到上面的$key文件中去并上传
// $key = '5.txt',//上传的文件名,可以不存在,下面的Body会给此文件写入内容
// $body = fopen('./hello.txt', 'rb'),//打开指定的文件把内容写入到上面的$key文件中去并上传
// $options = array(
// "ACL"=>'private',
// 'CacheControl' => 'private'));
// print_r($result);
// } catch (\Exception $e) {
// echo "$e\n";
// }
// 下载文件到本地
$result = $cosClient->getObject(array(
//bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
'Bucket' => $bucket,
'Key' => '5.txt',//服务器下载的文件
'SaveAs' => './444.txt'));//下载之后的文件路径及新命名
echo($result);
}
//另外上传方式,使用form表单上传
$appid = config('file.tengxun_cos.appid');
$bucket = config('file.tengxun_cos.bucket');
$SecretId = config('file.tengxun_cos.SecretId');
$SecretKey = config('file.tengxun_cos.SecretKey');
if ($request->hasFile('file')) {
$image = $request->file('file');
print_r($image);
$cosClient = new \Qcloud\Cos\Client(
array(
'region' => 'ap-beijing',
'credentials' => array(
'appId' => $appid,
'secretId' => $SecretId,
'secretKey' => $SecretKey
)
)
);
// $result_upload = $cosClient->upload(
// $bucket = $bucket,
// $key = time() . '_' . rand(1000000, 9999999) . '.' . $image->getClientOriginalExtension(),
// $body = $image->getRealPath()
//// $options = array(
//// "ACL" => 'private',
//// 'CacheControl' => 'private'
//// )
// );
$result_upload = $cosClient->putObject(
array(
'Bucket' => $bucket,
'Key' => time() . '_' . rand(1000000, 9999999) . '.' . $image->getClientOriginalExtension(),
//'Body' => $image
'Body' => file_get_contents($image->getRealPath())//此处非常重要,需要获得文件路径,使用文件流上传
)
// $options = array(
// "ACL" => 'private',
// 'CacheControl' => 'private'
// )
);
// $result_upload = object_to_array($result_upload);
print_r($result_upload);
exit;
} else {
$response = array(
'status' => 500,
'message' => '请选择要上传的文件'
);
}
return $response;
}