1
votes

I am writing a simple php application to test read/write preparations to a Cloud Storage bucket

The bucket is defined and works perfectly when accessed through console web interface

But although I have defined composer.json, php.ini and app.yaml follwing documentation, at run time i get the following message:

Warning: file_put_contents(): Unable to find the wrapper "gs" - did you forget to enable it when you configured PHP?

The code is

<?php

require_once '../vendor/autoload.php';

$destination = "gs://contest17014.appspot.com/ripetiOra_82833hd93hd9dh.mp3";
file_put_contents($destination, $output); 

?>

where $output is a mp3 file generated locally (not included for clarity)

I read into docs that wrapper should be included automatically, but this is written into a standard env document, while it is not mentioned into flexible env documentation. Should I define this wrapper by my self ?

3

3 Answers

1
votes

I believe you have your file_put_content parameters switched around, they should be:

<?php

require_once '../vendor/autoload.php';

$destination = "gs://contest17014.appspot.com/ripetiOra_82833hd93hd9dh.mp3";
file_put_contents($destination, $output); 
?>
1
votes

gs:// supported in standard environment only. For flex or other environment, you have to use StorageClient library.

Include google/cloud-datastore library via composer.

Example:

$ composer require google/cloud-storage

<?php
require 'vendor/autoload.php';

use Google\Cloud\Storage\StorageClient;

$storage = new StorageClient([
    'projectId' => 'my_project'
]);

$bucket = $storage->bucket('my_bucket');

// Upload a file to the bucket.
$bucket->upload(
    fopen('/data/file.txt', 'r')
);

// Using Predefined ACLs to manage object permissions, you may
// upload a file and give read access to anyone with the URL.
$bucket->upload(
    fopen('/data/file.txt', 'r'),
    [
        'predefinedAcl' => 'publicRead'
    ]
);

// Download and store an object from the bucket locally.
$object = $bucket->object('file_backup.txt');
$object->downloadToFile('/data/file_backup.txt');
0
votes

For php72 and standard env

use Google\Cloud\Storage\StorageClient; 
function register_stream_wrapper($projectId) {   
    $client = new StorageClient(['projectId' => $projectId]);
    $client->registerStreamWrapper();
}
register_stream_wrapper("projectId");

Thanks to James Crowley