gpt4 book ai didi

php - 使用 php api 在谷歌云存储中插入图像

转载 作者:可可西里 更新时间:2023-11-01 00:40:38 24 4
gpt4 key购买 nike

我正在尝试在谷歌云平台中设置存储。老实说,现在我有点困惑该怎么做。

我正在使用以下代码。

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']
);

在执行该函数时出现以下错误。

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

我只想将图片上传到谷歌存储。

最佳答案

请务必使用可以从 GCP 下载的 json 格式的凭据。

在标题中我有这个:

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']);

希望对您有所帮助!

关于php - 使用 php api 在谷歌云存储中插入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40445681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com