Just wondering if anyone knows of a prewritten php script to to transfer files from rackspace cloud site to rackspace cloud files.
Rackspace does provide a script to backup files to the cloud but not transfer. (and I only realized that after spending a couple of hours and finally getting the script working).
http://www.rackspace.com/knowledge_center/article/how-to-use-cron-to-backup-cloudsites-to-cloudfiles
I don't know PHP (which is required for Rackspace cron jobs), so if there's a script that would help me with this it would be great.
Thanks.
Below is the script I use when I backup to rackspace. It uses the php cloud files master library from racker labs. I set it up as a simple cron. Simply replace the emai
php 'path/to/backup.php'
<?
require_once('php-cloudfiles-master/cloudfiles.php');
$server_name = 'YOUR_SERVER_NAME'; //Name of the Current Server
$curr_date_time = date("Y-m-d--H:i:s"); //DON' CHANGE - Date
$curr_day = date("Yd"); //DON' CHANGE - Date
$sites = array("ENTERDATABASES HERE IN ARRAY FORMAT"); //Array of Databases to be Backed up
$site_root = "/var/www/";
$temp_dir = "/bak/mysql/"; //temp directory
$site_temp_dir = "/bak/site/"; //temp directory
$rscfUsername = 'YOUR RACKSPACE USERNAME'; // the username for your rackspace account
$rscfApiKey = 'YOUR RACKSPACE API KEY'; // the api key for your account
$rscfContainer = 'YOUR RACKSPACE CONTAINER'; //rackspace containr
foreach($sites as $site) {
try{
exec("tar -czpf {$site_temp_dir}{$site}{$curr_date_time}_site.tar.gz {$site_root}{$site}");
// check if the file is created
if(file_exists("{$site_temp_dir}{$site}{$curr_date_time}_site.tar.gz")) {
$auth = new CF_Authentication($rscfUsername, $rscfApiKey);
$auth->ssl_use_cabundle();
$auth->authenticate();
$conn = new CF_Connection($auth);
$conn->ssl_use_cabundle();
// I allready created a container with this name otherwise user create_container
$container = $conn->get_container($rscfContainer);
// make a unique name with date in it so you know when it was created
$objectName = $site.$curr_date_time.'.tar.gz';
$store = $container->create_object($objectName);
$store->load_from_filename("{$site_temp_dir}{$site}{$curr_date_time}_site.tar.gz"); // send to rackspace
}
} catch (Exception $e) {
print_r($e, true);
}
}
?>
Related
I am new to Azure and i'm trying to list all objects within a blob with an extension of .json
I can do this easily in AWS and it works perfectly. I've been trying to find an equivalent command in Azure.
Here is what i've tried. It only returns the first .json file.
I need to list all the .json files in all folders ( I know they are not really folders but hopefully people understand what i'm trying to do )
$containerName = 'calls';
$result = $blobClient->listBlobs($containerName, $listBlobsOptions);
foreach ($results as $result) {
$keyname = $result['keyname'];
echo "\n Key name is ".$keyname; // I can see the keyname
if (strpos($keyname, 'meta-data.json') !== false) {
$id = $result['id'];
echo "\n" . $count;
$blobUrl = $keyname; // I get the blob url returned but its only ever the first level
$metadata = file_get_contents($blobUrl); // tried to get the file.. no success , another issue to address later
echo $metadata;
}
}
Any help would be greatly appreciated.
You can use the below code to list blobs in a folder by using azure php SDK:
// List blobs.
$key = 'keyname';
$blobListOptions = new ListBlobsOptions();
$blobListOptions->setPrefix($key);
$blobList = $blobRestProxy->listBlobs($blobContainer, $blobListOptions);
foreach($blobList->getBlobPrefixes() as $key => $blob) {
echo "BlobPrefix ".$key.": \t".$blob->getName()."\n";
}
foreach($blobList->getBlobs() as $key => $blob) {
echo "Blob ".$key.": \t".$blob->getName()."\t(".$blob->getUrl().")\n";
}
Thanks to Microsoft for providing more information on Azure PHP SDKs in GitHub here.
Iam dealing with kafka and php, Im beginner the kafka has been set upped correctly on the server,
now am on local
I have installed:
librdkafka.dll on xamp/php
and php_rdkafka.dll on xamp/php/ext
and i added this on php.ini:
extension=php_rdkafka.dll
then, i have created a php page contains this code:
$topic_name = '???';
$conf = new RdKafka\Conf();
$conf->set('metadata.broker.list', $broker_list);
$conf->set('enable.idempotence', 'true');
$conf->set('group.id', $topic_name);
$conf->set('sasl.username', "???");
$conf->set('sasl.mechanism', "PLAIN");
$conf->set('sasl.password', "???");
$conf->set('log_level', (string) LOG_DEBUG);
$conf->set('debug', 'all');
$producer = new RdKafka\Producer($conf);
$producer->addBrokers($broker_list);
$topic = $producer->newTopic($topic_name);
$topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message Test");
$producer->poll(0);
$result = $producer->flush(10000);
but nothing returns on stream
Are you trying to connect to confluent cloud?
I would add to your conf your security protocol, as you indicate sasl. eg $conf->set('security.protocol','sasl_ssl');
I would think you would not need to indicate the brokers in the producer, as you have already done this in the config. I am presuming your broker list is a csv formatted file that include host and port number?
I want to run a single webpage to display files, which are stored in an Azure File Storage. It must be Azure File storage because these files came from a Docker container, which is mounted, to that file storage.
I have a Azure Container Instance witch stores PDF files in an Azure File Storage. Now I run a WebApp (PHP) that shall read all the PDFs.
I’ve installed the Microsoft Azure Storage File PHP SDK but have no clue how to use it. Even the sample.php did not help me coming forward. It would be very helpful if someone could help me a bit a simple sniped.
I see you want to directly display a PDF file from Azure File Storage in a web page. Generally, the best practice is to generate the url with sas token of a file in Azure File Storage.
So I followed the GitHub repo Azure/azure-storage-php to install Azure File Storage SDK for PHP in my sample project, here is my sample code and its dependencies.
The file structure of my sample project is as the figure below.
The content of my composer.json file is as below.
{
"require": {
"microsoft/azure-storage-file": "*"
}
}
My sample PHP file demo.php is as below, that's inspired by the function generateFileDownloadLinkWithSAS of the offical sample azure-storage-php/samples/FileSamples.php.
<?php
require_once "vendor/autoload.php";
use MicrosoftAzure\Storage\Common\Internal\Resources;
use MicrosoftAzure\Storage\File\FileSharedAccessSignatureHelper;
$accountName = "<your account name>";
$accountKey = "<your account key>";
$shareName = "<your share name>";
$fileName = "<your pdf file name>";
$now = date(DATE_ISO8601);
$date = date_create($now);
date_add($date, date_interval_create_from_date_string("1 hour"));
$expiry = str_replace("+0000", "Z", date_format($date, DATE_ISO8601));
$helper = new FileSharedAccessSignatureHelper($accountName, $accountKey);
$sas = $helper->generateFileServiceSharedAccessSignatureToken(
Resources::RESOURCE_TYPE_FILE,
"$shareName/$fileName",
'r', // Read
$expiry // A valid ISO 8601 format expiry time, such as '2020-01-01T08:30:00Z'
);
$fileUrlWithSAS = "https://$accountName.file.core.windows.net/$shareName/$fileName?$sas";
echo "<h1>Demo to display PDF from Azure File Storage</h1>";
echo "<iframe src='$fileUrlWithSAS' width='800' height='500' allowfullscreen webkitallowfullscreen></iframe>";
?>
The result of my web page is as the figures below in Chrome and Firefox.
The result in Chrome:
The result in Firefox:
Update: The code to list files in a file share, as below.
<?php
require_once "vendor/autoload.php";
use MicrosoftAzure\Storage\File\FileRestProxy;
$accountName = "<your account name>";
$accountKey = "<your account key>";
$shareName = "<your share name>";
$connectionString = "DefaultEndpointsProtocol=https;AccountName=$accountName;AccountKey=$accountKey";
$fileClient = FileRestProxy::createFileService($connectionString);
$list = $fileClient->listDirectoriesAndFiles($shareName);
function endsWith( $str, $sub ) {
return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
}
foreach($list->getFiles() as &$file) {
$fileName = $file->getName();
if(endsWith($fileName, ".pdf")) {
echo $fileName."\n";
}
};
?>
I have set up an windows azure website (php), and I want to connect to the azure storage (blob) environment. I walked through the How to use the Blob service from PHP tutorial, but that only mentions the case when the website is stored localy.
I tried to set up a few cases, but i'm constantly getting a http 500 error.
<?php
require_once 'WindowsAzure/WindowsAzure.php';
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;
//$connectionString = "\WindowsAzure\Blob\Internal\IBlob";
// Create blob REST proxy.
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString); // the code gets stuck at this line, result is a HTTP 500 error
$content = fopen("C:\Users\Public\Pictures\Sample%20Pictures\Woestijn.jpg", "r");
$blob_name = "newBlob";
try {
//Upload blob
$blobRestProxy->createBlockBlob("default", $blob_name, $content);
}
catch(ServiceException $e){
// Handle exception based on error codes and messages.
// Error codes and messages are here:
// http://msdn.microsoft.com/en-us/library/windowsazure/dd179439.aspx
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}?>
Is there anyone who had a similar problem and managed to figure it out?
EDIT:
I now narrowed down the error search. I went into the ServicesBuilder.php file, and commented out line by line, until the page stopped to work. The line it went wrong at is $httpClient, as shown below:
public function createBlobService($connectionString)
{
$settings = StorageServiceSettings::createFromConnectionString(
$connectionString
);
$httpClient = $this->httpClient();
$serializer = $this->serializer();
$uri = Utilities::tryAddUrlScheme(
$settings->getBlobEndpointUri()
);
From what I'm seeing you're filling up the $connectionString variable with this value: "\WindowsAzure\Blob\Internal\IBlob" (even though it's commented - so probably you're passing it from somewhere else). If that's the case you'll need to change it.
The connection string should be a reference to your storage account containing the protocol, the name of the account and the key (you can find the name and the key in the portal):
$connectionString = "DefaultEndpointsProtocol=https;AccountName=jeroensaccount;AccountKey=fjmezfjmIOFJEZIOPAFJAZOPIFJAIMO"
I am trying to script some code that will search a specified folder on the server for the latest file with a specific file extension (in my case .zip) and transfer that file to Rackspace's Cloud Files. The code below is as far i got and i keep getting the error:
Fatal error: Uncaught exception 'IOException' with message 'Could not open file for reading: Resource id #8' in /home/test/public_html/cloudapi/cloudfiles.php:1952 Stack trace: #0 /home/test/public_html/final.php(60): CF_Object->load_from_filename(Resource id #8) #1 {main} thrown in /home/test/public_html/cloudapi/cloudfiles.php on line 1952
The code that i'm using below was originally done for uploading content via an html upload form & i'm trying to adapt the same code to use a local server file instead of an uploaded file. You will see commented code with was previously for an upload script to show you how the upload script had worked.
<?php
// include the Cloud API.
require('cloudapi/cloudfiles.php');
// START - Script to find recent file with the extension .zip in current folder
$show = 2; // Leave as 0 for all
$dir = ''; // Leave as blank for current
if($dir) chdir($dir);
$files = glob( '*.zip');
usort( $files, 'filemtime_compare' );
function filemtime_compare( $a, $b )
{
return filemtime( $b ) - filemtime( $a );
}
$i = 0;
foreach ( $files as $file )
{
++$i;
if ( $i == $show ) break;
$value = $file; //Variable $value contains the filename of the recent file to be used in Cloud Files API
}
// END - Script to find recent file with the extension .zip in current folder
// START - Rackspace API code to upload content to cloud files container
// Rackspace Connection Details;
$username = "randomusername"; // put username here
$key = "234887347r93289f28h3ru283h2fuh23093402398"; // api key
// Connect to Rackspace
$auth = new CF_Authentication($username, $key);
$auth->authenticate();
$conn = new CF_Connection($auth);
//Set the Container you want to use
$container = $conn->get_container('Backups');
//Temp store the file
//$localfile = $_FILES['uploadfile']['tmp_name'];
//$filename = $_FILES['uploadfile']['name'];
$localfile = fopen($value, "r");
$filename = $value;
//Uploading to Rackspace Cloud
$object = $container->create_object($filename);
$object->load_from_filename($localfile);
echo "Your file has been uploaded";
// END - Rackspace API code to upload content to cloud files container
?>
I know this is an old thread. But for the benefit of people who may land at this page while searching for a solution...
The problem with your code is:
The following statement opens the file for reading
$localfile = fopen($value, "r");
However when you place the load_from_filename call, the routine again tries to open the same file and fails because you already have it open in $localfile.
So comment out the previous command and you should be able to run the script successfully.
Error's being thrown in because content type not defined from read file.