对象相关接口 代码示例 生成上传预签名URL: php public function generatePutobjectPresignedUrl() { $bucket ' '; $objectName ' '; $expires time() + 300; // 当前时间戳加上300秒,表示5分钟后过期 $cmd $this>s3Client>getCommand('PutObject', [ 'Bucket' > $bucket, 'Key' > $objectName, ]); $request $this>s3Client>createPresignedRequest($cmd, $expires); $presignedUrl (string)$request>getUri(); echo "generatePutobjectPresignedUrl success " . $presignedUrl . "n"; } 通过该预签名URL,可以直接将文件上传到指定的桶: php public function putObjUsingPresignedUrl($presignedUrl, $localFilePath) { if (!fileexists($localFilePath)) { echo "File does not exist: " . $localFilePath . "n"; return; } $file fopen($localFilePath, 'r'); $ch curlinit($presignedUrl); curlsetopt($ch, CURLOPTPUT, true); curlsetopt($ch, CURLOPTINFILE, $file); curlsetopt($ch, CURLOPTINFILESIZE, filesize($localFilePath)); $result curlexec($ch); if ($result false) { echo "Upload failed: " . curlerror($ch) . "n"; } else { echo "Upload successful. File uploaded from: " . $localFilePath . "n"; } // 关闭资源 curlclose($ch); fclose($file); } 请求参数 参数 类型 说明 是否必要 Bucket string 桶名称 是 Key string 对象key 是 expires intstring 过期时间(Unix时间戳或者能用strtotime解析的字符串) 是 返回结果 生成对应的预签名上传 URL,该链接允许用户在指定的时间内直接将对象上传到媒体存储存储桶。 Post上传
来自: