首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Google云存储代码不起作用

Google云存储代码不起作用
EN

Stack Overflow用户
提问于 2018-05-28 15:36:03
回答 1查看 724关注 0票数 1

在Google Cloud中创建了下面的代码,用于将图像从我的服务器上传到google cloud,但我在使用google存储类时遇到错误

下面是我的upload_gcs.php代码

require 'vendor/autoload.php';

use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Core\Exception\GoogleException;

if (isset($_FILES) && $_FILES['file']['error']== 0) {
$allowed = array ('png', 'jpg', 'gif', 'jpeg');
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (!in_array(strtolower ($ext), $allowed)) {
echo 'The file is not an image.';
die;
}

$projectId = 'photo-upload-205311';

$storage = new StorageClient ([
'projectId' => $projectId,
'keyFilePath' => 'Photo Upload-3af18f61531c.json'
]);

$bucketName = 'photo-upload-205311.appspot.com';
$bucket = $storage->bucket($bucketName);

$uploader = $bucket-> getResumableUploader (
fopen ($_FILES['file']['tmp_name'], 'r'),[
'name' => 'images/load_image.png',
'predefinedAcl' => 'publicRead',
]);

try {
$uploader-> upload ();
echo 'File Uploaded';
} catch (GoogleException $ex) {
$resumeUri = $uploader->getResumeUri();
$object = $uploader->resume($resumeUri);
echo 'No File Uploaded';
}
}
else {
echo 'No File Uploaded';
}

我得到的错误如下

> Warning: The use statement with non-compound name
> 'GoogleCloudStorageStorageClient' has no effect in upload_gcs.php on
> line 4
> 
> Fatal error: Class 'StorageClient' not found in upload_gcs.php on line
> 16

我的过程是否正确,或者是否有任何其他方法将图像从我的服务器上传到google云存储。

EN

回答 1

Stack Overflow用户

发布于 2018-05-28 19:42:16

必须使用脚本的正确名称空间,否则将无法解析。请参阅下面的更正。

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

use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Core\Exception\GoogleException;

class GCPStorage {
    function __construct()
    {
        $projectId = '<your-project-id>';
        $bucketName = '<your-bucket-name>'; 
        $storage = new StorageClient([
            'projectId' => $projectId,
            'keyFilePath' => '<your-service-account-key-file>'
        ]);

        $this->bucket = $storage->bucket($bucketName);
    }

    function uploadToBucket()
    {
        if(/your-precondition/) {
            return 'No File Uploaded';
        }

        $uploadedFileLocation = $_FILES['file']['tmp_name'];
        $uploader = $this->bucket->getResumableUploader(
            fopen($uploadedFileLocation, 'r'),
            ['name' => 'images/file.txt', 'predefinedAcl' => 'publicRead']
        );
        try { 
            $object = $uploader->upload();
        } catch(GoogleException $ex) {
            $resumeUri = $uploader->getResumeUri();
            try {
                $object = $uploader->resume($resumeUri);
            } catch(GoogleException $ex) {
                return 'No File Uploaded';
            }
        } finally {
            return 'File Uploaded';
        }
    }
}

$gcpStorage = new GCPStorage;

echo $gcpStorage->uploadToBucket();

一个小建议:通过在前置条件失败时提早返回,将前提条件作为保护子句进行断言。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50561329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档