Reputation: 226
I am trying to setup storage in google cloud platform. Honestly now i am kind of confused on what to do.
I am using the following code.
require_once("vendor/autoload.php");
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Storage\StorageObject;
use Google\Cloud\Storage;
$projectId = 'YOUR_PROJECT_ID';
$storage = new StorageClient([
'projectId' => 'xx',
'key'=> 'yy'
]);
$file_name = "imagename";
$obj = new StorageObject();
$obj->setName($file_name);
$storage->objects->insert(
"kmapsimage",
$obj,
['name' => $file_name, 'data' => file_get_contents("https://kmapst.blob.core.windows.net/images/kmap581b939a7a28c.jpeg"),'mimeType' => 'image/jpeg']
);
On executing the function i get the following error.
Argument 1 passed to Google\Cloud\Storage\StorageObject::__construct() must implement interface Google\Cloud\Storage\Connection\ConnectionInterface, none given, called in /var/www/html/test_imageupload.php on line 35 and defined in /var/www/html/vendor/google/cloud/src/Storage/StorageObject.php on line 71
I just want to upload images to google storage.
Upvotes: 3
Views: 1407
Reputation: 1146
Be sure that you use your credentials that you can download from GCP in json format.
In the header I have this:
putenv("GOOGLE_APPLICATION_CREDENTIALS=".$_SERVER['DOCUMENT_ROOT']."/uploads/my-fe1tyui28f4f.json");
require $_SERVER['DOCUMENT_ROOT'].'/uploads/php-docs-samples/storage/api/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient();
// Primera imagen......
$file = fopen($_SERVER['DOCUMENT_ROOT']."/uploads/".$filename."_200.jpg", 'r');
$bucket = $storage->bucket("my_bucket");
$object = $bucket->upload($file, ['name' => $dir."/".$filename."_200.jpg"]);
$object = $bucket->object($dir."/".$filename."_200.jpg");
$object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
I hope this helps!
Upvotes: 3
Reputation: 771
The docs show a simpler way
Given your relevant code:
$storage = new StorageClient([
'projectId' => 'xx',
'key'=> 'yy'
]);
// Using the upload method seems a bit more straightforward
$bucket = $storage->bucket('bucket-name');
$file = fopen('path/to/file', 'r');
$object = $bucket->upload($file, ['name' => 'fileName']);
Upvotes: 2