将图像文件上传到谷歌云桶

时间:2018-02-01 09:04:21

标签: php file-upload google-cloud-storage fopen

现在,我想将图像文件上传到gcp bucket,因为我正在使用此代码:

require __DIR__ . '\vendor\autoload.php';

$storage = new StorageClient();
$file = fopen($params['book']['tmp_name'], 'r');
$bucket = $storage->bucket('fingertips-books');
$object = $bucket->upload($params['book']['name'], [
    'name' => 'test.pdf'
]);

$params变量包含此

的位置
Array
    (
    [type] => PDF
    [description] => dsds
    [book] => Array
        (
            [name] => 2017-03-23.pdf
            [type] => application/pdf
            [tmp_name] => C:\xampp\tmp\php97AA.tmp
            [error] => 0
            [size] => 45022
        )
)

1 个答案:

答案 0 :(得分:2)

Unless you're running on app engine using application default credentials, you'll have to set GOOGLE_APPLICATION_CREDENTIALS environment variable to point to a service account key file. See the example here.