Download Cloudinary data from EC2 - php

I use PHP Symfony for my backend. I need to download cloudinary data into my EC2 instance using the cloudinary url of a single file or folder. For that, i wanted to create a PHP script to do that.
I use cloudinary_php package (https://packagist.org/packages/cloudinary/cloudinary_php). I use the upload functionality to store my data, but it doesn't seem to have a download method.
I wanted to know if this is possible, with cloudinary_php or with another pachage ?
Thanks for your help!

In your Symfony's project root folder, you should have the file composer.json. Then add following entry that looks like this:
{
"require": {
"cloudinary/cloudinary_php": "^2"
}
}
Then make sure to install the dependencies. Follow this composer doc. After that, you can now then plug in your API key and Secret then instantiate a Cloudinary object:
require 'vendor/autoload.php';
use Cloudinary\Configuration\Configuration;
use Cloudinary\Api\Upload\UploadApi;
// configure globally via a JSON object
Configuration::instance([
'cloud' => [
'cloud_name' => 'your-cloud-name-here',
'api_key' => 'xxxxxxxx',
'api_secret' => 'xxxxxxxxxx'
],
'url' => [
'secure' => true
]
]);
//Instanstiate and generate an archive
$cloudinary = (new UploadApi());
$response = $cloudinary->createZip([
'tags' => 'jeep', // Change this base on your use case
'resource_type' => 'image' // Change this base on your use case
]);
//Check the response object
print_r($response);
//Make your own implementation here to download the archive.
The response object above should have the secure_url key where you can directly download the generated archive link. Visit the documentation here for more info. There are also lots of optional parameters you can pass and you should choose what works best for you. You should also consider Symfony's security best practices when referencing sensitive information. For general Cloudinary PHP SDK integration, visit this.

Related

How to set env in controller for dacastro4 laravel-gmail package?

I am trying to implement Gmail API for CRM based on laravel, where users can store multiple Google credentials, and using those credentials users can log in with their Google account.
I used dacastro4 laravel-gmail package, but for dacastro4/laravel-gmail package by default design, those Google credentials are stored in .env file of the laravel project.
.env
GOOGLE_PROJECT_ID=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URI=
`
I tried setting the .env variable in the controller constructor function, but not working.
for example,
env('GOOGLE_PROJECT_ID',$project_id);
//OR
putenv("GOOGLE_PROJECT_ID=".$project_id);
//OR
config(['GOOGLE_PROJECT_ID' => $project_id])
Also tried setting in the vendor dacastro4 laravel-gmail package, but the database model is not accessible.
How can I set multiple Google credentials from the controller?
Thank You.
You can set this data using the config() method, seeing as that's how Laravel accesses .env variables.
Create a config file for your variables:
config/gmail.php
<?php
return [
'project_id' => env('GOOGLE_PROJECT_ID'),
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect_url' => env('GOOGLE_REDIRECT_URI', '/'),
]
Then set values in your controller on the go using:
config(['gmail.project_id' => $project_id]);
and retrieve the values using:
config('gmail.project_id');

Laravel Push Notifications, non static method called statically

I am trying to implement this library for sending push notifications to iOS app. All my configurations are fine. When I tested the code snippet available at this page like this:
PushNotification::app('appNameIOS')
->to($deviceToken)
->send('Hello World, i`m a push message');
It threw this error:
Non-static method
Davibennun\LaravelPushNotification\PushNotification::Message() should
not be called statically
Rightly so, because when I opened the class, there was no such static method. There is one but that is not static. What am I doing wrong? Any help?
Edit 1
I have generated config file:
return array(
'hasalty_ios' => array(
'environment' =>'development',
'certificate' =>base_path('pem.p12'),
'passPhrase' =>'',
'service' =>'apns'
),
'hasalty_android' => array(
'environment' =>'production',
'apiKey' =>'yourAPIKey',
'service' =>'gcm'
)
);
Edit 2
My Laravel version is 5.5.31.
If you configure the library correctly, you should use
use Pushnotification;
instead of
use Davibennun\LaravelPushNotification\PushNotification;
When a user references any static method on the Cache facade, Laravel resolves the cache binding from the service container and runs the requested method (in this case, get) against that object.
How Facades Work
Edit
You must generate the config file before you use it:
php artisan vendor:publish --provider="Davibennun\LaravelPushNotification\LaravelPushNotificationServiceProvider" --tag="config"
Try to use this code:
Change here use Pushnotification instead
of use Davibennun\LaravelPushNotification\PushNotification;

Laravel googlemapper package, Cannot get variables from the config file, "Google maps API key is required." ERROR

I was using your plugin these days trying to implement a googlemap on my site. I followed all installation steps and set a variety of variables. I am on laravel 5.4.
I stuck in this error: "Google maps API key is required.". Even I try to hard set the API in your core function, then it throws another error says "region is required". seems like the config file cannot be reached. I also tried to fix the file permissions, but unfortunately that's not the issue. Do you have any ideas about this? This is part of my controller file
use Cornford\Googlmapper\Mapper;
use Cornford\Googlmapper\Facades\MapperFacade;
use Illuminate\Support\Facades\Route;
public function index(Mapper $mapper)
{
............
$firstMapItem = reset($mapArrays);
$mapper->map(
$firstMapItem->latitude, $firstMapItem->longitude,
[
'zoom' => 10,
'markers' => ['title' => 'My Location', 'animation' => 'DROP'],
'clusters' => ['size' => 10, 'center' => true, 'zoom' => 20]
]
);
// Add information window for each address
foreach ($mapArrays as $mapArray) {
$mapper->marker($mapArray->latitude, $mapArray->longitude);
}
}
Route
Route::get( '/map', 'MapController#index');
view
<div style="width: 100%; height: auto;">
{!! Mapper::render() !!}
</div>
BTW, I already changed the API value there, my problem is this API value has not been referenced. But if I move the Mapper function from the controller to the Route file, everything is fine. But if I do what I did as above, the API value from the googlmapper.php file isn't referenced.
Finally, make it works. Not because of the API issue, the API has been correctly placed.
change
use Cornford\Googlmapper\Mapper;
to
use Mapper;
Very simply Facades things I think.
credit to #zubinkadva on GitHub. Also the package author #bradcornford also mentioned
"however for reference you can also use the following:
use Cornford\Googlmapper\Facades\MapperFacade as Mapper;
"
From the package docs:
You also need to set your Google API Key into the GOOGLE_API_KEY environment variable.
So, get the API key first here.
Then add this line in the .env file in Laravel project root:
GOOGLE_API_KEY=your_real_api_key_here
As Docs said you have to add this
GOOGLE_API_KEY="your google api key here"
in your .env file in the root of your project to get this key you have to go in Google Console

Single sign on using SimpleSamlPhp wrapper on Laravel

Implementing single sign on in my laravel application. I have decided to use this plugin https://github.com/aacotroneo/laravel-saml2 which is basically a wrapper on famous SimpleSamlPhp.
I downloaded the code via composer and as per given information Remember that you don't need to implement those routes, but you'll need to add them to your IDP configuration. For example, if you use simplesamlphp, add the following to /metadata/sp-remote.php
$metadata['http://laravel_url/saml/metadata'] = array(
'AssertionConsumerService' => 'http://laravel_url/saml/acs',
'SingleLogoutService' => 'http://laravel_url/saml/sls',
//the following two affect what the $Saml2user->getUserId() will return
'NameIDFormat' => 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent',
'simplesaml.nameidattribute' => 'uid'
);
I can't find metadata/sp-remote.php, any idea? and as far as http://laravel_url/saml/acs is concerned, do I need to deploy saml on the server? because at the moment the plugin code is in vendors in laravel core architecture code hierarchy.
First some background:
There are two parts to any SAML interaction - the Identity Provider ("IDP") and the Service Provider ("SP"). The IDP is the master authenticator if you like, to which various applications (SPs) connect.
The idea is that the user visits your app, which in turn communicates as a Service Provider to the Identity Provider to get your credentials. And because multiple apps / SPs connect to the same IDP, you get the benefits of a single sign-on.
During the set-up phase, metadata configurations are swapped between the SPs and IDP to establish trust between them. This isn't user-level data -- it's application-level data that allows them to talk.
OK. So now on to your question:
The package you are using allows your Laravel app to talk to an IDP, but before it can do so you need to swap some metadata. The metadata for your app is the snippet above. This needs to go in the IDP configurations, which is where you will find this metadata/sp-remote (or more precisely metadata/saml20-sp-remote, which is where you paste this in.
If you haven't done so already, I'd recommend using [https://simplesamlphp.org/docs/stable/][1] as the IDP here as the Laravel package works with it pretty much out of the box.
One final tip: if you are using SAML2, then I found that you need to change the metadata key to refer to saml2 instead of saml above. ie $metadata['http://laravel_url/saml2/metadata'] and not $metadata['http://laravel_url/saml/metadata']
I hope this will help others. I added saml2_settings.php in the config folder.
Updated the routes:
'logoutRoute' => '/logout',
'loginRoute' => '/homepage',
'errorRoute' => '/error',
updated x509cert (publickey.cer) and privateKey
Updated 'entityId', added the url of metadata xml.
Updated singleLogoutService and rest of the required details in the saml2_settings.php file.
Added two listeners
1) for login event
2) for logout event
Updated the routes file like this:
\Illuminate\Support\Facades\Event::listen('Aacotroneo\Saml2\Events\Saml2LogoutEvent', function ($event) {
\Illuminate\Support\Facades\Auth::logout();
\Illuminate\Support\Facades\Session::save();
return redirect("login");
});
\Illuminate\Support\Facades\Event::listen('Aacotroneo\Saml2\Events\Saml2LoginEvent', function (\Aacotroneo\Saml2\Events\Saml2LoginEvent $event) {
$user = $event->getSaml2User();
$userData = [
'id' => $user->getUserId(),
'attributes' => $user->getAttributes(),
'assertion' => $user->getRawSamlAssertion()
];
// add the login for auto login based on your settings
/// REDIRECT the user to homepage
}
});

upload a file to s3 using Laravel, but using metadata login

Hi I am using laravel to upload a file to s3 bucket.
on config->filesystems we have following details for s3.
's3' => [
'driver' => env('FILE_DRIVER'),
'region' => env('S3_REGION'),
'bucket' => env('S3_BUCKET'),
],
on .env file we have following values
FILE_DRIVER=s3
S3_REGION=bucket region
S3_BUCKET=Bucket name
note that we are not using any
'key' => 'your-key',
'secret' => 'your-secret',
we just try to use the AWS metadata login in order to authenticate user and upload the file.
My problem is, how to use the existing file upload feature on Laravel in this case?
If the environment is Local or s3, is there a way that we can use the same code to upload the image.
Can anyone guide me how to do it? i am clueless here....
if more information needed, let me know
Use Laravel Storage class as following.
You can call methods on s3 using $s3 object.
use Storage;
$s3 = Storage::disk('s3');

Categories