Closed leonboot closed 4 months ago
Hi @leonboot , I am unable to get the setup working on my end. I used a test ssh server from https://sdf.org (repro script) but my ssh_exec
commands hangs indefinitely. Wondering whether you could face this issue too!
On the GCS library's end, we just do the following operations on the stream:
GuzzleHttp\Psr7\Utils::streamFor($stream)
GuzzleHttp\Psr7\MultipartStream
and send over to network wire via GuzzleHttp\Psr7\Request
.So, ssh streams are non-seekable (see stream_get_meta_data
responds ["seekable"]=> bool(false)
). Because the GCS lib has read the stream & saved it, you can either fetch it back OR wrap it into a different variable to re-read it (untested):
$seekable_stream = fopen('php://memory','r+');
fwrite($seekable_stream, $stream);
rewind($seekable_stream);
Also, I see that you've enabled stream_set_blocking
so you might be better off by upload the string output of SSH commands to GCS instead (consider using phpseclib3\Net\SSH2->exec(...)
). I would consider this as a more reliable workaround unless your use-case really needs to use streaming ssh output.
Closing due to no response.
I'm trying to upload a stream's contents directly to a bucket using the
StorageClient->bucket('bucket_name')->upload()
method. The stream is a resource created by thessh2_exec()
command (I want to stream the result of an SSH command directly to a file in a GCS bucket).I need to determine the exit code of the executed command to determine whether the command was ran successfully. This can be done by calling the
stream_get_meta_data()
function on the stream resource. However, this results in an error after the stream was read by theStorageClient->bucket('bucket_name')->upload()
method. Runningis_resource()
on the stream resource before and after passing it to theupload()
command results in atrue
andfalse
, respectively. This leads me to believe that theupload()
methods, or any of the methods called by this method, closes the stream once it is finished reading it. This leaves me unable to read the stream's metadata. I believe this is undesired.Here's a script to reproduce the issue:
The above script will result in the following output:
If the above test script is changed so that the
upload()
call is replaced by the following code:Then the output is as follows:
In other words, just consuming the stream by calling
fgets()
on it untilEOF
is reached keeps the resource intact and its metadata can still be read. When calling$bucket->upload()
on it seems to close the resource before the metadata can be read.