I am using mpdf library to convert HTML to PDF and successfully stored my pdf file on local as well as remote server. But I don't want to store my pdf files on my code repos on server and like to utilize storage bucket available on google cloud.
/*
*/
private function generatePDF($params, $quotationId) {
$location = '/var/www/html/development/pdfs/';
$html = $this->load->view('quotation', $data, TRUE);
$filename = "quo_" .time() . ".pdf";
$mpdf = new \Mpdf\Mpdf(['mode' => 'en-IN', 'format' => 'A4']);
$mpdf->WriteHTML($html);
$mpdf->SetHTMLFooter('<p style="text-align: center; text-size: 12px;">This is computer generated quotation. It does not require signature.</p>');
$pdf = $mpdf->Output($location . $filename, 'F');
$this->UploadModel->upload($pdf, $filename);
}
public function upload($pdf, $pdfName) {
$storage = new StorageClient();
$bucket = $storage->bucket("bucketname");
$object = $bucket->upload($pdf, ['name' => $pdfName]);
$object = $bucket->object($pdfName);
$object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
}
Here I have used 'F' type in which it saves the pdf file in pdfs folder created in my code repo hosted on cloud server but I would like to directly store it to Google cloud storage bucket.
I am not having much experience about google cloud and mpdf library so looking for help and guidance to achieve the functionality.
Please kindly help me.
I see you are using Cloud Storage Client Libraries for PHP.
First, you need to install it to your machine:
composer require google/cloud-storage
And then you need to set up authentication by following the guide.
Once these are set create a bucket to store the PDFs.
Then replace your upload function with the code from the documentation:
use Google\Cloud\Storage\StorageClient;
/**
* Upload a file.
*
* #param string $bucketName the name of your Google Cloud bucket.
* #param string $objectName the name of the object.
* #param string $source the path to the file to upload.
*
* #return Psr\Http\Message\StreamInterface
*/
function upload_object($bucketName, $objectName, $source)
{
$storage = new StorageClient();
$file = fopen($source, 'r');
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($file, [
'name' => $objectName
]);
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
}
i also faced same issue & came out with this solution, i hope it will help you.
use 'S' instead of 'F'parameter, so it will return string data & pass this data directly into upload method.
Related
I am using PHP to upload image to firebase storage. the picture is being uploaded but it is not being accessible as i have to manually create " access token " to make it accessible.
here is the code im using
$bucketName = "example.appspot.com";
$objectName = 'Photos/test.jpeg';
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload(fopen('sign.jpeg', 'r'),
[
'name' => $objectName
]
);
That is indeed working as expected: since your upload is not going through a Firebase SDK, there is not method to generate a download URL.
The common workaround is to create a signed URL with an expiration time far into the future, which is the closest equivalent that Cloud Storage has to Firebase's download URL.
In addition to #Frank's answer, you could also assign the publicRead ACL to the uploaded file and compose the public URL manually:
$bucketName = "example.appspot.com";
$objectName = 'Photos/test.jpeg';
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload(fopen('sign.jpeg', 'r'), [
'name' => $objectName
'predefinedAcl' => 'publicRead'
]);
$publicUrl = "https://{$bucket->name()}.storage.googleapis.com/{$object->name()}";
I have made an indirect way to generate and store the access token.
$payload = file_get_contents('https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/Photos%2Fpic.jpeg');
$data = json_decode($payload);
echo $data->downloadTokens;
This code has created the access token and it shows the downloadToken on screen.
Thank you everyone for your answers.
How do I upload images to firebase cloud storage? The documentation gives only these methodes but no upload method. This is the documentation link https://firebase-php.readthedocs.io/en/stable/cloud-storage.html
$storage = $factory->createStorage();
$storageClient = $storage->getStorageClient();
$defaultBucket = $storage->getBucket();
I have seen another stack question related but don't understand the answer.
I would also like to get a link to the stored file.
Thank you in advance!
Check official firebase documentation, as is mentioned there:
"To upload a file to Cloud Storage, you first create a reference to the full path of the file, including the file name."
For example:
// Create a root reference
var storageRef = firebase.storage().ref();
// Create a reference to 'mountains.jpg'
var mountainsRef = storageRef.child('mountains.jpg');
// Create a reference to 'images/mountains.jpg'
var mountainImagesRef = storageRef.child('images/mountains.jpg');
// While the file names are the same, the references point to different files
mountainsRef.name === mountainImagesRef.name // true
mountainsRef.fullPath === mountainImagesRef.fullPath // false
Also, I found another thread here where you can find an example using php
You can do something like this,
$storage = new StorageClient();
$file = fopen($source, 'r');
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($file, [
'name' => $objectName
]);
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
There examples on gcp github repository.
file upload example : here
other examples: here
i want to upload a file to google cloud storage using google client php library on github. Am able to upload file to cloud storage but am not able to upload to a directory in cloud storage. i get the error message No such object: bucketName/abc/test.jpg
$client = new Google_Client();
putenv('GOOGLE_APPLICATION_CREDENTIALS=files/google_cloud.json');
$client->useApplicationDefaultCredentials();
$storage = new Google\Cloud\Storage\StorageClient([
'projectId' => $googleprojectID
]);
$sPath = "files/com/test.jpg";
$objectName = "/abc/test.jpg";
$bucketName = $googlebucketName;
$bucket = $storage->bucket($bucketName);
$bucket->upload( fopen($sPath, 'r') );
$object = $bucket->object($objectName);
$info = $object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
First of all, let me share with you this documentation page where you will find the complete reference for the Google Cloud Storage PHP Client Library. More specifically, if you have a look at the upload() method, you will see that in order to set the name of the object uploaded (and therefore its location, given that GCS has a flat namespace), you have to use the options parameter, which can contain a name field pointing to the right location to upload.
Also, note that the correct object name should not start with a slash /, given that it will automatically be added after the bucket name. Therefore, you should modify your code to add something like this:
$sPath = "files/com/test.jpg";
$objectName = "abc/test.jpg"; # Note the removal of "/" here
$options = [
'name' => $objectName
];
$bucketName = $googlebucketName;
$bucket = $storage->bucket($bucketName);
$bucket -> upload(
fopen($sPath, 'r'),
$options
);
i am new in firebase web if it possible to upload, download, and delete file using php. i have upload file using JS but i want to download using PHP.
Here is script of download file using JS but i want in PHP.
Thanks in advance...
My Code
[START storage_quickstart]
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';
# Imports the Google Cloud client library
use Google\Cloud\Storage\StorageClient;
# Your Google Cloud Platform project ID
$projectId = 'My project ID';
# Instantiates a client
$storage = new StorageClient([
'projectId' => $projectId
]);
# The name for the new bucket
$bucketName = 'my bucket';
# Creates the new bucket
$bucket = $storage->createBucket($bucketName);
echo 'Bucket ' . $bucket->name() . ' created.';
# [END storage_quickstart]
return $bucket;
The short answer is that you should use gcloud-php. This requires that you set up a service account (or use Google Compute Engine/Container Engine/App Engine which provide default credentials).
It's likely that you'll create a service account, download a keyfile.json, and provide it as an argument to the StorageClient, like so:
# Instantiates a client
$storage = new StorageClient([
'keyFilePath' => '/path/to/key/file.json',
'projectId' => $projectId
]);
Alternatively, it looks like they've built another layer of abstraction, which takes the same arguments but allows you to use lots of other services:
use Google\Cloud\ServiceBuilder;
$gcloud = new ServiceBuilder([
'keyFilePath' => '/path/to/key/file.json',
'projectId' => 'myProject'
]);
$storage = $gcloud->storage();
$bucket = $storage->bucket('myBucket');
That's an old question, but I was struggling with same problem... hope my solution help someone.
In fact, I really don't know if there is an official way to do that, but I created the method below and it worked for me.
function storageFileUrl($name, $path = []) {
$base = 'https://firebasestorage.googleapis.com/v0/b/';
$projectId = 'your-project-id';
$url = $base.$projectId.'/o/';
if(sizeof($path) > 0) {
$url .= implode('%2F', $path).'%2F';
}
return $url.$name.'?alt=media';
}
To access files in the root of bucket:
$address = storageFileUrl('myFile');
Result: https://firebasestorage.googleapis.com/v0/b/your-project-id.appspot.com/o/myFile?alt=media
To access files inside some folder, do:
$address = storageFileUrl('myFile', ['folder', 'subfolder']);
Result: https://firebasestorage.googleapis.com/v0/b/your-project-id.appspot.com/o/folder%2Fsubfolder%2FmyFile?alt=media
Enjoy.
Is it possible to convert from private s3 files in bucket to public using PHP library provided by Amazon AWS S3?
All you need to do is set the ACL to public-read, you can do this with the PHP SDK using the update_object() function.
$s3 = new AmazonS3();
$bucket = 'my-bucket' . strtolower($s3->key);
$response = $s3->update_object($bucket, 'test1.txt', array(
'acl' => AmazonS3::ACL_PUBLIC
));
Source