I have been trying for a long time to upload some invoices that my application (which is running in app engine) creates as pdf, to a bucket in GCS. Later on, the app will send the same by email to each customer. It is working well when running the application in php from my local PC, connecting to App Engine instance. But whenever I am deployin the full code, it does not work at all. I have my credential file for the service account (which the original one created by defaul for my project) in folder in my folder structure of the application. I point to it anytime I am trying to acces the bucket in GCS regardless I am working locally or in GAE. As I say everything goes smooth form local, but nothing from cloud. Here is some code.
//FILE TO UPLOAD TO GCS
date_default_timezone_set('Europe/Madrid');
require __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../cn.php';
require_once __DIR__ . '/../librerias/fpdf/fpdf.php';
if(!isset($_SESSION)){
session_start();
}
use Google\Cloud\Storage\StorageClient;
putenv('GOOGLE_APPLICATION_CREDENTIALS=../librerias/credentials/credentials.json');
function upload_object($bucketName, $objectName, $source){
$storage = new StorageClient();
$file = $source;
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($file, [
'name' => $objectName
]);
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
}
//############### CODE GENERATING PDF FILE #######################3
//blablablablablablabla
// ################ END OF PDF FILE GENERATION USING FPDF##################
$pdfData=$pdf->Output('S','',1);
$filename="Facturas/".$ano."/".$factura_1."-".$ano.".pdf";
$bucketName="my-bucket.appspot.com";
upload_object($bucketName, $filename, $pdfData);//THIS IS NOT WORKING WHEM RUNNING FROM GAE, BUT WORKING WELL FROM LOCAL CONNECTING TO MY INSTANCE THROUGH PROXY
//############### CODE SENDING PDF FILE USING SENGRID #######################3
//blablablablablablabla
// ################ END OF PDF FILE SENDING ##################
//END OF FILE UPLOAD TO GCS
//APP.YAML
runtime: php72
runtime_config:
document_root: .
handlers:
# Serve a directory as a static resource.
- url: /Css
static_dir: Css
# Serve a directory as a static resource.
- url: /font
static_dir: font
# Serve a directory as a static resource.
- url: /librerias
static_dir: librerias
- url: /librerias/credentials
static_dir: librerias/credentials
# Serve images as static resources.
- url: /icons/(.+\.(gif|png|jpg|jpeg|ico))$
static_files: icons/\1
upload: icons/.+\.(gif|png|jpg|jpeg|ico)$
- url: /img/(.+\.(gif|png|jpg|jpeg))$
static_files: img/\1
upload: img/.+\.(gif|png|jpg|jpeg)$
- url: /img/Logos/(.+\.(gif|png|jpg|jpeg))$
static_files: img/Logos/\1
upload: img/Logos/.+\.(gif|png|jpg|jpeg)$
- url: .*
script: auto
secure: always
entrypoint:
serve handler.php
Solved, I finally got it working. The issue seems to be how to authorize my service account. Instead of
putenv('GOOGLE_APPLICATION_CREDENTIALS=../librerias/credentials/credentials.json');
It is working with
$storage = new StorageClient([
'keyFile' => json_decode(file_get_contents('mycredentials.json'), true)
]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.