简体   繁体   English

我无法从 App Engine 将任何文件上传到 GCS 中的存储桶,但我可以使用 php 从本地服务器编码

[英]I can't upload any file to bucket in GCS from App Engine, but I can from my local server coding with php

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.我一直在尝试将我的应用程序(在应用程序引擎中运行)创建为 pdf 的一些发票上传到 GCS 中的存储桶。 Later on, the app will send the same by email to each customer.稍后,该应用程序将通过 email 将相同的内容发送给每个客户。 It is working well when running the application in php from my local PC, connecting to App Engine instance.从我的本地 PC 运行 php 中的应用程序时,它运行良好,连接到 App Engine 实例。 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.无论我是在本地工作还是在 GAE 中工作,每当我试图访问 GCS 中的存储桶时,我都会指出它。 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)
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 App Engine 中的云 shell 部署 GCS 中我的应用程序存储桶中的文件? - How do I deploy the files from my app bucket in GCS from cloud shell in App engine? 如何将json数组从服务器上的php文件传递到我的Ionic应用程序? - how can i pass json array from php file on a server to my ionic app? 无法使用 PHP 将图像从应用程序上传到 Hostinger 服务器 - Can't upload image from app to Hostinger server using PHP 如何在Google App Engine中将我的应用程序上传到后端 - How can i upload my app to backend in google app engine 如何从我的服务器 [PHP] 读取 .txt 文件? - How can I read .txt file from my server [PHP]? 我无法从我的PHP文件接收JS中的数据 - I can't receive data in JS from my PHP file 如何使用PHP备份服务器文件将文件从服务器上传到Google云存储到Google服务器? - How can I upload files to google cloud storage from my server to google's server using PHP to backup my server files? Google App Engine jQuery无法访问我的php文件 - google app engine jQuery can't access my php file 如何使用PHP从iPhone上传视频 - How I can upload videos from my iPhone with PHP 将图像从iOS应用程序上传到php-不太正确-我缺少什么? - Upload image from iOS app to php — Can't quite get it right — What am I missing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM