Example #1
1
 /**
  * Get a read-stream for a file
  *
  * @param   string  $path
  * @return  array   file metadata
  */
 public function readStream($path)
 {
     if (!in_array('s3', stream_get_wrappers())) {
         $this->client->registerStreamWrapper();
     }
     $context = stream_context_create(array('s3' => array('seekable' => true)));
     $stream = fopen('s3://' . $this->bucket . '/' . $this->prefix($path), 'r', false, $context);
     return compact('stream');
 }
 function writeCsv(array $headerRow, array $resultsArray)
 {
     $this->s3Client->registerStreamWrapper();
     $exportBucketPath = $this->getOutputFilePath($this->exportPathWithProtocol);
     $this->writeToDestination($exportBucketPath, $headerRow, $resultsArray);
     return $exportBucketPath;
 }
Example #3
0
 /**
  * @param S3Client $client
  * @param array    $options
  */
 public function __construct(S3Client $client, $options = [])
 {
     parent::__construct($options);
     // We need to register the S3 stream wrapper so that we can take advantage of the base class
     $this->client = $client;
     $this->client->registerStreamWrapper();
 }
Example #4
0
 /**
  * Constructor
  *
  * @access public
  * @param  string $key    AWS API Key
  * @param  string $secret AWS API Secret
  * @param  string $region AWS S3 Region
  * @param  string $bucket AWS S3 bucket
  * @param  string $prefix Object prefix
  */
 public function __construct($key, $secret, $region, $bucket, $prefix)
 {
     $this->bucket = $bucket;
     $credentials = new Credentials($key, $secret);
     $this->client = new S3Client(['credentials' => $credentials, 'region' => $region, 'version' => '2006-03-01']);
     $this->client->registerStreamWrapper();
     $this->prefix = $prefix;
 }
Example #5
0
 protected function init()
 {
     if (!$this->options['accesskey'] || !$this->options['secretkey'] || !$this->options['signature'] || !$this->options['region'] || !$this->options['bucket']) {
         return $this->setError('Required options undefined.');
     }
     $this->s3 = S3Client::factory(['key' => $this->options['accesskey'], 'secret' => $this->options['secretkey'], 'signature' => $this->options['signature'], 'region' => $this->options['region']]);
     $this->s3->registerStreamWrapper();
     $this->root = $this->options['path'];
     $this->rootName = 's3';
     return true;
 }
Example #6
0
 public function download($bucket, $folder, $return_file_count = false)
 {
     $this->client = S3Client::factory(["key" => AWS_NETSUITE_INTEGRATION_KEY, "secret" => AWS_NETSUITE_INTEGRATION_SECRET]);
     try {
         $files = [];
         $results = [];
         $this->client->registerStreamWrapper();
         $dir = "s3://{$bucket}/{$folder}/tmp";
         if (is_dir($dir) && ($dh = opendir($dir))) {
             while (($file = readdir($dh)) !== false) {
                 if (is_file($dir . DS . $file)) {
                     $files[] = $file;
                 }
             }
             closedir($dh);
         }
         if ($return_file_count) {
             return count($files);
         }
         foreach ($files as $import) {
             $results[] = ['data' => json_decode(file_get_contents($dir . DS . $import)), 'file' => $dir . DS . $import];
         }
         return $results;
     } catch (Exception $e) {
         //$this->out("There was an error getting files");
     }
 }
Example #7
0
 /**
  * When providing the $source argument, you may provide a string referencing
  * the path to a directory on disk to upload, an s3 scheme URI that contains
  * the bucket and key (e.g., "s3://bucket/key"), or an \Iterator object
  * that yields strings containing filenames that are the path to a file on
  * disk or an s3 scheme URI. The "/key" portion of an s3 URI is optional.
  *
  * When providing an iterator for the $source argument, you must also
  * provide a 'base_dir' key value pair in the $options argument.
  *
  * The $dest argument can be the path to a directory on disk or an s3
  * scheme URI (e.g., "s3://bucket/key").
  *
  * The options array can contain the following key value pairs:
  *
  * - base_dir: The directory to remove from the filename when saving.
  * - before: A callable that accepts the following positional arguments:
  *   source, dest, command; where command is an instance of a Command
  *   object. The provided command will be either a GetObject, PutObject,
  *   InitiateMultipartUpload, or UploadPart command.
  * - mup_threshold: Size in bytes in which a multipart upload should be
  *   used instead of PutObject. Defaults to 20971520 (20 MB).
  * - concurrency: Number of files to upload concurrently. Defaults to 5.
  * - debug: Set to true to print out debug information for transfers. Set
  *   to an fopen() resource to write to a specific stream.
  *
  * @param S3Client         $client  Client used for transfers.
  * @param string|\Iterator $source  Where the files are transferred from.
  * @param string           $dest    Where the files are transferred to.
  * @param array            $options Hash of options.
  */
 public function __construct(S3Client $client, $source, $dest, array $options = [])
 {
     $client->registerStreamWrapper();
     if (is_string($source)) {
         $this->base_dir = $source;
         $source = Utils::recursiveDirIterator($source);
     } elseif (!$source instanceof \Iterator) {
         throw new \InvalidArgumentException('source must be the path to a ' . 'directory or an iterator that yields file names.');
     } elseif (!$this->base_dir) {
         throw new \InvalidArgumentException('You must provide the source ' . 'argument as a string or provide the "base_dir" option.');
     }
     $valid = ['mup_threshold', 'base_dir', 'before', 'concurrency', 'debug'];
     foreach ($valid as $opt) {
         if (isset($options[$opt])) {
             $this->{$opt} = $options[$opt];
         }
     }
     if ($this->mup_threshold < 5248000) {
         throw new \InvalidArgumentException('mup_threshold must be >= 5248000');
     }
     // Normalize the destination and source directory.
     $this->dest = rtrim(str_replace('\\', '/', $dest), '/');
     $this->base_dir = rtrim(str_replace('\\', '/', $this->base_dir), '/');
     $this->destScheme = $this->getScheme($this->dest);
     $this->sourceScheme = $this->getScheme($this->base_dir);
     $this->client = $client;
     if ($this->destScheme == 's3') {
         $this->s3Args = $this->getS3Args($this->dest);
     }
     if ($this->debug) {
         $this->wrapDebug();
     }
     $this->source = $this->wrapIterator($source);
 }
 /**
  * @throws Exception
  */
 public function init()
 {
     if (empty($this->credentials)) {
         throw new Exception('S3 credentials isn\'t set.');
     }
     if (empty($this->region)) {
         throw new Exception('Region isn\'t set.');
     }
     if (empty($this->bucket)) {
         throw new Exception('You must set bucket name.');
     }
     if (!empty($this->cdnHostname)) {
         $this->cdnHostname = rtrim($this->cdnHostname, '/');
     }
     $args = $this->prepareArgs($this->options, ['version' => '2006-03-01', 'region' => $this->region, 'credentials' => $this->credentials, 'debug' => $this->debug]);
     $this->client = new S3Client($args);
     // to use PHP functions like copy(), rename() etc.
     // https://docs.aws.amazon.com/aws-sdk-php/v3/guide/service/s3-stream-wrapper.html
     $this->client->registerStreamWrapper();
 }
 /**
  * @return S3Client
  */
 public function connect()
 {
     if (!$this->client) {
         // Create an Amazon S3 client object
         $this->client = S3Client::factory(array('key' => $this->key, 'secret' => $this->secret));
         if ($this->region) {
             $this->client->setRegion($this->region);
         }
         // Register the stream wrapper from a client object
         $this->client->registerStreamWrapper();
         $this->scheme = 's3://' . $this->bucketName;
         if (file_exists($this->scheme)) {
             $this->scheme .= '/' . $this->baseFolder;
             if (!file_exists($this->scheme)) {
                 $this->filesystem->mkdir($this->scheme);
             }
         } else {
             throw new \Exception('Bucket ' . $this->bucketName . ' missing.');
         }
     }
     return $this->client;
 }
Example #10
0
$id = 0;
$level = 0;
if ($row) {
    $id = $row[0];
    $level = $row[1];
}
if ($id == 0) {
    $error = 2;
}
if ($error == 0) {
    header('Access-Control-Allow-Origin: *');
    switch ($level) {
        case 1:
            // Create a client object
            $client = new S3Client(['version' => 'latest', 'region' => 'us-west-2']);
            $client->registerStreamWrapper();
            if (isset($graph)) {
                readfile("s3://{$bucket}/{$folder}/{$graph}.json") or $error = 1;
            }
            break;
        case 2:
            $demo = '2007-08-voyage-mineralogy-biota-433724';
            if ($graph == $demo) {
                readfile("{$demo}.json") or $error = 1;
            } else {
                $error = 2;
            }
            break;
        default:
            $error = 2;
    }
 /**
  * @param array    $config
  * @param S3Client $s3
  * @codeCoverageIgnore
  */
 public function __construct(array $config, S3Client $s3)
 {
     $this->s3 = $s3;
     $this->bucket = $config['bucket'];
     $this->s3->registerStreamWrapper();
 }
Example #12
0
 /**
  * Initialize the S3 Client
  *
  * @return void
  */
 public function initializeObject()
 {
     $clientOptions = $this->s3DefaultProfile;
     $this->s3Client = S3Client::factory($clientOptions);
     $this->s3Client->registerStreamWrapper();
 }
Example #13
0
 public function downloadObject($bucket, $key)
 {
     $this->bucket = $bucket;
     $this->key = $key;
     $this->scenario = 'download';
     if ($this->validate()) {
         try {
             $client = new S3Client(['version' => 'latest', 'region' => Yii::$app->params['amazon']['region'], 'credentials' => Yii::$app->params['amazon']['credentials']]);
             $client->registerStreamWrapper();
             return file_get_contents('s3://' . $bucket . '/' . $key);
         } catch (\Exception $e) {
             Yii::error('Error getting file content from S3. Bucket - ' . $this->bucket . ' Key - ' . $this->key . ' Extra - ' . $e->getMessage());
             return null;
         }
     }
     return null;
 }