/** * Changes how buckets are referenced in the HTTP request * * @param Event $event Event emitted */ public function onCommandAfterPrepare(Event $event) { $command = $event['command']; $bucket = $command['Bucket']; $request = $command->getRequest(); $pathStyle = false; $command['PathStyle'] = $this->path_style; // Skip operations that do not need the bucket moved to the host. if (isset(self::$exclusions[$command->getName()])) { return; } if ($key = $command['Key']) { // Modify the command Key to account for the {/Key*} explosion into an array if (is_array($key)) { $command['Key'] = $key = implode('/', $key); } } // Set the key and bucket on the request $request->getParams()->set('bucket', $bucket)->set('key', $key); // Switch to virtual if PathStyle is disabled, or not a DNS compatible bucket name, or the scheme is // http, or the scheme is https and there are no dots in the host header (avoids SSL issues) if (!$command['PathStyle'] && $command->getClient()->isValidBucketName($bucket) && !($command->getRequest()->getScheme() == 'https' && strpos($bucket, '.'))) { // Switch to virtual hosted bucket $request->setHost($bucket . '.' . $request->getHost()); $request->setPath(preg_replace("#^/{$bucket}#", '', $request->getPath())); } else { $pathStyle = true; } if (!$bucket) { $request->getParams()->set('s3.resource', '/'); } elseif ($pathStyle) { // Path style does not need a trailing slash $request->getParams()->set('s3.resource', '/' . rawurlencode($bucket) . ($key ? '/' . ObsClient::encodeKey($key) : '')); } else { // Bucket style needs a trailing slash $request->getParams()->set('s3.resource', '/' . rawurlencode($bucket) . ($key ? '/' . ObsClient::encodeKey($key) : '/')); } }
/** * Create a canonicalized resource for a request * * @param RequestInterface $request Request for the resource * * @return string */ private function createCanonicalizedResource(RequestInterface $request) { $buffer = $request->getParams()->get('s3.resource'); // When sending a raw HTTP request (e.g. $client->get()) if (null === $buffer) { $bucket = $request->getParams()->get('bucket') ?: $this->parseBucketName($request); // Use any specified bucket name, the parsed bucket name, or no bucket name when interacting with GetService $buffer = $bucket ? "/{$bucket}" : ''; // Remove encoding from the path and use the S3 specific encoding $path = ObsClient::encodeKey(rawurldecode($request->getPath())); // if the bucket was path style, then ensure that the bucket wasn't duplicated in the resource $buffer .= preg_replace("#^/{$bucket}/{$bucket}#", "/{$bucket}", $path); } // Remove double slashes $buffer = str_replace('//', '/', $buffer); // Add sub resource parameters $query = $request->getQuery(); $first = true; foreach ($this->signableQueryString as $key) { if ($query->hasKey($key)) { $value = $query[$key]; $buffer .= $first ? '?' : '&'; $first = false; $buffer .= $key; // Don't add values for empty sub-resources if ($value !== '' && $value !== false && $value !== null && $value !== QueryString::BLANK) { $buffer .= "={$value}"; } } } return $buffer; }
use Guzzle\Http\EntityBody; use Obs\S3\Exception\S3Exception; use Guzzle\Http\Message\Response; function func($var) { if (!$var) { $S3 = new S3Exception("hello word"); $S3->setExceptionCode(400); $S3->setExceptionType("Client"); $S3->setResponse(new Response(405)); throw $S3; } } // $r = ObsClient::isValidBucketName("123"); // var_dump($r); $client = ObsClient::factory(array('key' => 'C6840CE16E23C6F06EFF', 'secret' => 'RueC/EXFyvTbWGG6VkenF82w5pIAAAFNbiPG8EfN', 'endpoint' => 'http://129.7.182.2:5080', 'signature' => 's3')); try { func(0); } catch (S3Exception $e) { echo $e; } echo "--------------------------------\n"; //ssl认证 $client->setSslVerification(false, false, 0); $resp = $client->listBuckets(); var_export($resp); // $resp = $client->createBucket(array( // 'Bucket'=>'123456-2', // 'ACL'=>'log-delivery-write', // 'LocationConstraint'=>'abc' // ));
$bucket_name = '123456-10'; //桶名 $object_name = 'test'; //对象名 /**************************************************************************** * 函数原型:public static function factory($config = array()) * 函数功能:初始化服务工厂 * 参数说明:key:AK值 * secret:SK值 * endpoint:服务器地址, * signature:s3, * path_style:是否使用绝对路径,取值 true or false * region:登陆服务器的区域位置 * 返回值:初始化服务句柄 ***************************************************************************/ $client = ObsClient::factory(array('key' => 'D2DE0CBCFF0BEFBE5712', 'secret' => 'KUaA754YVbcnRgP9ap12WsIcp8UAAAFN/wvvvvHL', 'endpoint' => 'http://129.7.182.2:80', 'signature' => 's3', 'path_style' => true, 'region' => 'mdc')); /************************************************************************************ * 函数原型:listBuckets() * 函数功能:查询桶列表 * 参数说明: * 返回值:Buckets:查询桶信息列表,包含值(Name:桶名,CreationDate:创建时间) * Owner:桶拥有者信息,包含值(ID:拥有者用户ID,DisplayName:拥有者用户名) * RequestId:该消息的返回ID。 ***********************************************************************************/ function ListBuckets() { global $client; echo "list bucket start...\n"; try { $resp = $client->listBuckets(); printf("RequestId:%s\n", $resp['RequestId']);