AnsweredAssumed Answered

Challenge: Read file contents that is in a .tar.gz file from a remote server in the On-demand environment

Question asked by Chad Hutchins on Oct 5, 2016

We have an API that we work with that saves API responses in .tar.gz files on a remote AWS server. The problem with that is it is very difficult to download files and then do something with them in the On-demand Sugar environment. 

 

In a NON-On-demand Sugar environment with PHP 5.3 or above you can do something like this:

 

<?php

$url = "https://random.s3.amazonaws.com/some-tar-gz-file.tar.gz";
$file = "the-tar-gz-file.tar.gz";
$tarfile = "the-tar-gz-file.tar";
$dir = "extracted-direcory";
$finalfile = "file.txt";
file_put_contents($file, fopen($url, 'r'));

try
{
     $a = new PharData($file);
     $a->decompress(); // creates files.tar
     $b = new PharData($tarfile);
     $b->extractTo($dir,null,true);
     $contents = file_get_contents($dir.'/'.$finalfile);
}
catch(Exception $e)
{
     echo "Error: ".$e->getMessage();
}

// $contents contains the value of the file I'm after

$contents will contain the string I'm looking for, usually a JSON encoded string I can then json_decode and start using.

 

Pretty quickly you find that file_put_contents and file_get_contents will hit the Module Loader blacklist functions for an installable module that uses this, so this isn't an option.

 

The workaround I have in mind would be to use PHP Curl to get the contents of the .tar.gz file, then somehow in memory (do not create/read any files on the Sugar server) read the archive and get to the file contents I'm looking for. Ideally this would NOT use the PharData class unless you know a way to pass it contents of a .tar.gz file instead of using files on the server.

 

Anyone have any other ideas? Do you know how to read a .tar.gz file contents from memory in PHP?

Outcomes