Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Storage/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.51.0
2.0.0
2 changes: 1 addition & 1 deletion Storage/src/StorageClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class StorageClient
use ArrayTrait;
use ClientTrait;

const VERSION = '1.51.0';
const VERSION = '2.0.0';

const FULL_CONTROL_SCOPE = 'https://www.googleapis.com/auth/devstorage.full_control';
const READ_ONLY_SCOPE = 'https://www.googleapis.com/auth/devstorage.read_only';
Expand Down
21 changes: 19 additions & 2 deletions Storage/src/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* A streamWrapper implementation for handling `gs://bucket/path/to/file.jpg`.
* Note that you can only open a file with mode 'r', 'rb', 'rt', 'w', 'wb', 'wt', 'a', 'ab', or 'at'.
* Note that you can only open a file with mode 'r', 'rb', 'rt', 'w', 'wb', 'wt', 'a', 'ab', 'at', 'x', 'xb', or 'xt'.
*
* See: http://php.net/manual/en/class.streamwrapper.php
*/
Expand Down Expand Up @@ -211,7 +211,8 @@ public static function getClient($protocol = null)
* download the file to see if it can be opened.
*
* @param string $path The path of the resource to open
* @param string $mode The fopen mode. Currently supports ('r', 'rb', 'rt', 'w', 'wb', 'wt', 'a', 'ab', 'at')
* @param string $mode The fopen mode. Currently supports ('r', 'rb', 'rt',
* 'w', 'wb', 'wt', 'a', 'ab', 'at', 'x', 'xb', 'xt')
* @param int $flags Bitwise options STREAM_USE_PATH|STREAM_REPORT_ERRORS|STREAM_MUST_SEEK
* @param string $openedPath Will be set to the path on success if STREAM_USE_PATH option is set
* @return bool
Expand Down Expand Up @@ -264,6 +265,22 @@ public function stream_open($path, $mode, $flags, &$openedPath)
$options + ['name' => $name]
)
);
} elseif ($mode == 'x') {
try {
if ($this->bucket->object($this->file)->exists()) {
return $this->returnError('File already exists.', $flags);
}
} catch (ServiceException $ex) {
return $this->returnError($ex->getMessage(), $flags);
}

$this->stream = new WriteStream(null, $options);
$this->stream->setUploader(
$this->bucket->getStreamableUploader(
$this->stream,
$options + ['name' => $this->file, 'ifGenerationMatch' => 0]
)
);
} elseif ($mode == 'r') {
try {
// Lazy read from the source
Expand Down
26 changes: 26 additions & 0 deletions Storage/tests/System/StreamWrapper/WriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,30 @@ public function testStreamingWrite()

$this->assertFileExists($this->fileUrl);
}

public function testFwriteWithXMode()
{
$this->assertFileDoesNotExist($this->fileUrl);

$output = 'This is a test with x mode';
$fd = fopen($this->fileUrl, 'x');
$this->assertIsResource($fd);
$this->assertEquals(strlen($output), fwrite($fd, $output));
$this->assertTrue(fclose($fd));

$this->assertFileExists($this->fileUrl);
}

public function testFwriteWithXModeFailsIfExists()
{
$this->assertFileDoesNotExist($this->fileUrl);

// Create the file first
touch($this->fileUrl);
$this->assertFileExists($this->fileUrl);

// Try to open with 'x' mode, should fail
$fd = @fopen($this->fileUrl, 'x');
$this->assertFalse($fd);
}
}
35 changes: 35 additions & 0 deletions Storage/tests/Unit/StreamWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,45 @@ public function testOpeningNonExistentFileReturnsFalse()
*/
public function testUnknownOpenMode()
{
$fp = @fopen('gs://my_bucket/existing_file.txt', 'z');
$this->assertFalse($fp);
}

/**
* @group storageWrite
*/
public function testOpeningExistingFileWithXModeReturnsFalse()
{
$object = $this->prophesize(StorageObject::class);
$object->exists()->willReturn(true);
$this->bucket->object('existing_file.txt')->willReturn($object->reveal());

$fp = @fopen('gs://my_bucket/existing_file.txt', 'x');
$this->assertFalse($fp);
}

/**
* @group storageWrite
*/
public function testOpeningNonExistentFileWithXModeSucceeds()
{
$object = $this->prophesize(StorageObject::class);
$object->exists()->willReturn(false);
$this->bucket->object('new_file.txt')->willReturn($object->reveal());

$uploader = $this->prophesize(StreamableUploader::class);
$uploader->upload()->shouldBeCalled();
$uploader->getResumeUri()->willReturn('https://resume-uri/');

$this->bucket->getStreamableUploader(Argument::any(), Argument::withEntry('ifGenerationMatch', 0))
->willReturn($uploader->reveal());

$fp = fopen('gs://my_bucket/new_file.txt', 'x');
$this->assertIsResource($fp);
fwrite($fp, "some data");
fclose($fp);
}

/**
* @group storageRead
*/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
"google/cloud-spanner": "2.7.0",
"google/cloud-speech": "2.5.0",
"google/cloud-sql-admin": "1.8.0",
"google/cloud-storage": "1.51.0",
"google/cloud-storage": "2.0.0",
"google/cloud-storage-control": "1.8.0",
"google/cloud-storage-transfer": "2.4.0",
"google/cloud-storagebatchoperations": "0.7.0",
Expand Down
Loading