To be honest, I am really frustrated with the lack of document of Google Cloud Storage with PHP. Most of what I found in here (Stackoverflow) is out of date.
Here is my attempt:
$postbody = array('data' => file_get_contents('e.png'));
$gso = new Google_Service_Storage_StorageObject();
$gso->setName('testing');
$gso->setContentType('images/png');
$service->objects->insert($bucket_name, $gso, $postbody);
The error message
(400) Upload requests must be sent to /upload/*. Re-send request to the same path, but with an /upload prefix.
From Google Cloud Storage JSON API, I understand that I need to use upload/storage/v1/b/bucket_name/o instead of storage/v1/b/bucket_name/o, but how do I do it with Google Cloud Storage PHP Client API?
I managed to build a small script that uploads big files to GCS.
I used Google APIs Client Library for PHP
Find it in github https://github.com/guillefd/Backup-manager-gcs
This library manages the file upload (big files) https://github.com/guillefd/Backup-manager-gcs/blob/master/application/libraries/Googlecloudstorage.php
I´m actually using it in many sites and works quite fine.
Related
We have 2 different system that's hosted to 2 different server. Let's call them API and CRM. The system we have on API obviously is all APIs and the CRM is our main system. So we also have this website where we can create a data in our Database.
I'm having trouble in finding a way to put the uploaded file from API to CRM server. The code is $upload->path = $folder.DIRECTORY_SEPARATOR.$date_append.$filename; and it looks like this "path/to/api/[date]-filename"
since $folder = storage_path('uploads');
So how do I make it path/to/CRM/[date]-filename instead of API where it's originally created?
if your API server is public and you don't mind using it as hosting for the image then you can use it in the CRM like this ex: https://apiserverdomain.com/path/to/CRM/[date]-filename.
but the best practice is to use Cloud Storage (like AWS S3 or Google Cloud or even FTP) and display the URL in your CRM from the Cloud Storage directly
I hope it's helpful
Can we upload image files to a GCP running compute engine directly using PHP?
I know that it can be done using the backend ssh on GCP.
I have a solution that works in my local system to upload files but does not work on GCP.
Based on initial research it appears that GCP does not allow file upload directly.
Need confirmation though or if there is a way in which file upload can work, please enlighten.
I do not think it can be done directly through php, through ssh yes.
You can upload files with PHP.
If you want to upload to the server local disk, things become a bit simpler. Just ensure you have PHP configured for upload: PHP File Upload
If you want to upload to Google Storage, say to a bucket, you'd need to authenticate and get access there. Google Cloud SDK can provide this. Then you can install API Client Libraries for PHP and optional Cloud SDK components. That way you might call gcloud shell command or use API. Cloud SDK will provide transparent connection and authentication for you so that you can upload to the Google Storage.
Please see:
Cloud SDK
Google API Client Libraries
Currently I am using google app engine for my php project. I decided to migrate my project to Google Compute Engine, but i could not connect to Google Cloud Storage through php. Is there anyway to work around this issue?
Update:
I found that it is achievable and i need some example of using Server API to grab the storage content. This is what i did.
require_once 'GoogleAPI/autoload.php';
$projectId = "dummyid";
$client = new Google_Client();
$client->setApplicationName("Gave");
$client->setDeveloperKey("SERVERKEY");
$client->addScope(Google_Service_Storage::DEVSTORAGE_FULL_CONTROL);
$storage = new Google_Service_Storage($client);
$buckets = $storage->buckets->listBuckets($projectId);
foreach ($buckets['items'] as $bucket) {
printf("%s\n", $bucket->getName());
}
But it still doesnt work. Does server key has limited to access some control ? It gives me an error 500.
You can certainly use Google Cloud Storage (GCS) from Google Compute Engine (GCE). You'll find it's one of the very fastest ways to access GCS. You cannot, however, access GCS using the same App Engine PHP libraries when you're not using App Engine.
The canonical client library for using PHP from environments other than AppEngineis Google APIs Client Library for PHP. You can find demonstrations and tutorials on accessing GCS with that library here: https://cloud.google.com/storage/docs/json_api/v1/json-api-php-samples
As a second option, the new gcloud-php project has basic support for GCS.
Finally, as a third option, the public APIs for accessing GCS directly with HTTP requests are well documented and work very well from GCE.
I'm working on a PHP application hosted on Google App Engine which requires access to objects stored in an S3 Bucket. I've looked at the APIs available for Amazon S3 and all of them make use of CURL. But CURL is not allowed in Google App Engine.
Is there a way to access Bucket Contents and user other functions available in the S3 API without using CURL in PHP?
Amazon recently released a wrapper for file_get_contents which is what urlfetch uses - so take a look at http://blogs.aws.amazon.com/php/blog/tag/stream - looks like you can do $contents = file_get_contents("s3://{$bucket}/{$key}");
You should look at the URL Fetch docs - it's the google recommended way to do calls https://developers.google.com/appengine/docs/php/urlfetch/
Use the URL Fetch API instead of CURL
https://developers.google.com/appengine/docs/php/urlfetch/
I want to upload the photo in google cloud server instead of my server to avoid load on my sever. Also I want to fetch the images later in my server. I will do that in php and mysql. Is it possible to do that? If so then how shall I proceed? Please help me.
You can interact with Google Cloud Storage via its RESTful API or use a php library like tws_service_google_storage.