Superbalist / flysystem-google-cloud-storage

Flysystem Adapter for Google Cloud Storage
MIT License
265 stars 105 forks source link

PHP warnings while using via FlysystemStreamWrapper #76

Closed eeroniemi closed 5 years ago

eeroniemi commented 6 years ago

I'm not sure if this is problem with FlysystemStreamWrapper or this library, but using the similar code with AWS S3 adapter works fine. If problem is with FlysystemStreamWrapper I can open separate issue there.

Problem is that when I register GoogleCloudStorageAdapter as stream wrapper and write to file using file_put_contents file is uploaded fine but I get PHP warnings.

PHP version:

> php -v
PHP 7.1.13 (cli) (built: Jan  5 2018 15:31:15) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

composer.json

{
    "require": {
        "league/flysystem": "dev-master",
        "twistor/flysystem-stream-wrapper": "dev-master",
        "superbalist/flysystem-google-storage": "dev-master"
    }
}

test.php

<?php
use League\Flysystem\Filesystem;
use Twistor\FlysystemStreamWrapper;
use Google\Cloud\Storage\StorageClient;
use Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter;
require __DIR__ . '/vendor/autoload.php';

$keyFilePath = '/Users/eero/flystream/google-application-credentials.json';
$bucketName = 'yourbucketname';
$projectId = 'yourgoogleprojectid';
$basePath = 'sandbox';

$clientConfig = [
    'projectId'  => $projectId,
    'keyFilePath' => $keyFilePath
];

$client = new StorageClient($clientConfig);
$bucket = $client->bucket($bucketName);
$adapter = new GoogleStorageAdapter($client, $bucket, $basePath);

$filesystem = new Filesystem($adapter);
FlysystemStreamWrapper::register('eerotest', $filesystem);
$targetPath = 'eerotest://testfile';
file_put_contents($targetPath, 'foo');

Now when I run php test.php I would expect no errors, but what I actually get:

PHP Warning:  fseek(): supplied resource is not a valid stream resource in /Users/eero/flystream/vendor/twistor/flysystem-stream-wrapper/src/FlysystemStreamWrapper.php on line 421

Warning: fseek(): supplied resource is not a valid stream resource in /Users/eero/flystream/vendor/twistor/flysystem-stream-wrapper/src/FlysystemStreamWrapper.php on line 421
PHP Warning:  fclose(): supplied resource is not a valid stream resource in /Users/eero/flystream/vendor/twistor/flysystem-stream-wrapper/src/FlysystemStreamWrapper.php on line 387

Warning: fclose(): supplied resource is not a valid stream resource in /Users/eero/flystream/vendor/twistor/flysystem-stream-wrapper/src/FlysystemStreamWrapper.php on line 387
Richard87 commented 6 years ago

Hi!

I get the exact same error too:

Warning: fclose(): supplied resource is not a valid stream resource

$logger->info("Uplaoding local file", ['size' => $this->size, 'filename' => $this->filename]);
$fileSize = filesize($this->localfile);
if (!$fileSize)
     throw new \DomainException("Couldn't read filesize! " . error_get_last());

$this->size = $fileSize;
$fh = fopen($this->localfile, "rb");
if (!is_resource($fh))
    throw new \DomainException("Couldn't open file! " . error_get_last());

$success = $filesystem->writeStream($this->getRealPath(), $fh);
fclose($fh);
Richard87 commented 6 years ago

It might be tricky to solve, Guzzle6's Stream wrapper closes the stream on __destruct,

This creates a few issues when using Flysystems ReplicateAdapter when the Stream is closed prematurely :/

https://github.com/thephpleague/flysystem-replicate-adapter

TerraSkye commented 6 years ago

doesn't this issue resides with the twistor/flysystem-stream-wrapper package ?

Richard87 commented 6 years ago

Hi!

It's been a while since I looked at it, but I think this is the relevant file https://github.com/guzzle/psr7/blob/master/src/Stream.php#L78:

/**
 * Closes the stream when the destructed
 */
public function __destruct()
{
    $this->close();
}
bradjones1 commented 6 years ago

Refs: https://github.com/twistor/flysystem-stream-wrapper/pull/9

nicja commented 5 years ago

Closed as this issue is not related to this package