You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.0 KiB
PHTML
69 lines
2.0 KiB
PHTML
|
3 weeks ago
|
<?php
|
||
|
|
|
||
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||
|
|
|
||
|
|
use App\Services\Cloud\CloudObjectStorageS3Client;
|
||
|
|
use GuzzleHttp\Client;
|
||
|
|
use GuzzleHttp\HandlerStack;
|
||
|
|
use GuzzleHttp\Promise\Create;
|
||
|
|
use GuzzleHttp\Psr7\Response;
|
||
|
|
use Psr\Http\Message\RequestInterface;
|
||
|
|
|
||
|
|
$assert = static function (bool $condition, string $message): void {
|
||
|
|
if (!$condition) {
|
||
|
|
throw new RuntimeException($message);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
$path = tempnam(sys_get_temp_dir(), 'virt-template-put-');
|
||
|
|
if ($path === false) {
|
||
|
|
throw new RuntimeException('Cannot create immutable PUT fixture.');
|
||
|
|
}
|
||
|
|
file_put_contents($path, str_repeat('immutable-put-fixture-', 128));
|
||
|
|
$bytes = filesize($path);
|
||
|
|
$sha256 = hash_file('sha256', $path);
|
||
|
|
|
||
|
|
// A real HTTP handler may release its request body before request() returns.
|
||
|
|
// Closing the request body here deterministically reproduces that ownership
|
||
|
|
// boundary without crossing the network.
|
||
|
|
$handler = static function (RequestInterface $request, array $options) {
|
||
|
|
$request->getBody()->close();
|
||
|
|
return Create::promiseFor(new Response(200, [
|
||
|
|
'ETag' => '"closed-body-etag"',
|
||
|
|
'x-amz-version-id' => 'closed-body-version',
|
||
|
|
]));
|
||
|
|
};
|
||
|
|
$client = new CloudObjectStorageS3Client(new Client([
|
||
|
|
'handler' => HandlerStack::create($handler),
|
||
|
|
]));
|
||
|
|
|
||
|
|
try {
|
||
|
|
$identity = $client->putImmutableFile(
|
||
|
|
[
|
||
|
|
'endpoint' => 'https://objects.example.com',
|
||
|
|
'region' => 'ewr',
|
||
|
|
'bucket' => 'template-bucket',
|
||
|
|
'prefix' => 'virt-cloud',
|
||
|
|
'path_style' => true,
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'access_key_id' => 'TEMPLATEACCESSKEY',
|
||
|
|
'secret_access_key' => 'template-secret',
|
||
|
|
],
|
||
|
|
'virt-cloud/cloud-templates/template/generation/system.qcow2',
|
||
|
|
$path,
|
||
|
|
$bytes,
|
||
|
|
$sha256
|
||
|
|
);
|
||
|
|
} finally {
|
||
|
|
@unlink($path);
|
||
|
|
}
|
||
|
|
|
||
|
|
$assert($identity === [
|
||
|
|
'size_bytes' => $bytes,
|
||
|
|
'etag' => '"closed-body-etag"',
|
||
|
|
'version_id' => 'closed-body-version',
|
||
|
|
], 'Immutable PUT double-closed an HTTP-owned body stream.');
|
||
|
|
|
||
|
|
echo "Immutable object PUT stream ownership passed.\n";
|