/** * Get the signed download url of a file. * * @param string $path * @param int $expires * @param string $host_name * @param bool $use_ssl * @return string */ public function getSignedDownloadUrl($path, $expires = 3600, $host_name = '', $use_ssl = false) { $object = $this->applyPathPrefix($path); $url = $this->client->signUrl($this->bucket, $object, $expires); if (!empty($host_name) || $use_ssl) { $parse_url = parse_url($url); if (!empty($host_name)) { $parse_url['host'] = $this->bucket . '.' . $host_name; } if ($use_ssl) { $parse_url['scheme'] = 'https'; } $url = (isset($parse_url['scheme']) ? $parse_url['scheme'] . '://' : '') . (isset($parse_url['user']) ? $parse_url['user'] . (isset($parse_url['pass']) ? ':' . $parse_url['pass'] : '') . '@' : '') . (isset($parse_url['host']) ? $parse_url['host'] : '') . (isset($parse_url['port']) ? ':' . $parse_url['port'] : '') . (isset($parse_url['path']) ? $parse_url['path'] : '') . (isset($parse_url['query']) ? '?' . $parse_url['query'] : ''); } return $url; }
/** * 生成PutObject的签名url,主要用于私有权限下的写访问控制, 用户可以利用生成的signedUrl * 从文件上传文件 * * @param OssClient $ossClient OssClient实例 * @param string $bucket 存储空间名称 * @throws OssException */ function getSignedUrlForPuttingObjectFromFile($ossClient, $bucket) { $file = __FILE__; $object = "test/test-signature-test-upload-and-download.txt"; $timeout = 3600; $options = array('Content-Type' => 'txt'); try { $signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "PUT", $options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print __FUNCTION__ . ": signedUrl: " . $signedUrl . "\n"; $request = new RequestCore($signedUrl); $request->set_method('PUT'); $request->add_header('Content-Type', 'txt'); $request->set_read_file($file); $request->set_read_stream_size(filesize($file)); $request->send_request(); $res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code()); if ($res->isOK()) { print __FUNCTION__ . ": OK" . "\n"; } else { print __FUNCTION__ . ": FAILED" . "\n"; } }