[ create a new paste ] login | about

Link: http://codepad.org/KQZrsXTQ    [ raw code | output | fork ]

PHP, pasted on Jul 8:
<?php

namespace ScPortal\Invoices\Controller\Adminhtml\Download;

use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

class Index extends \Magento\Backend\App\Action
{
    var $keyname = 'sample'; // M&#7895;i file ph&#7843;i t&#7841;o 1 key
    var $bucket = 'kmj-localstack-ap-northeast-1';
    var $aryConfig = [
        'version' => '2006-03-01',
        'credentials' => [
            'key'    => 'demo',
            'secret' => 'demo',
        ],
        'region'  => 'ap-northeast-1',
        'endpoint' => 'http://10.0.3.241:4572'
    ];

    protected $resultPageFactory;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__('Invoice CSV Download'));

        $post = (array) $this->getRequest()->getParams();
        if (isset($post["region"])) {
            $this->keyname = $post['region'].'_'.$post['year'].'_'.$post['month'].'.csv';
            $this->upLoad();
            $this->download();
            error_log(print_r($post, true)."\n", 3, '/srv/www/var/log/log_datnn.txt');
        }
        return $resultPage;
    }

    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('ScPortal_Invoices::download');
    }

    public function upLoad()
    {
        // $filepath should be absolute path to a file on disk
        $filePath = '/srv/www/pub/static/adminhtml/Magento/backend/en_US/images/example.csv';
        // Instantiate the client.
        $s3 = S3Client::factory($this->aryConfig);
        try {
            // Upload data.
            $result = $s3->putObject(array(
                'Bucket' => $this->bucket,
                'Key'    => $this->keyname,
                'SourceFile'   => $filePath,
                'ACL'    => 'public-read',
                'ContentType' => 'text/csv',
                'Body' => fopen($filePath, 'rb') #đọc file ở mode binary
            ));
            //error_log(print_r($result, true) . "\n\n", 3, '/srv/www/var/log/log_aws_up.txt');
        } catch (S3Exception $e) {
            echo $e->getMessage() . "\n";
        }
    }

    public function download()
    {
        $s3v2 = S3Client::factory($this->aryConfig);
        $s3v2->doesObjectExist($this->bucket, $this->keyname);
        if(isset($s3v2)) {
            $cmd = $s3v2->getObject([
                'Bucket' => $this->bucket,
                'Key'    => $this->keyname
            ]);
            Header("Content-Type: " . $cmd->get('ContentType'));
            Header("Content-Disposition: attachment; filename=" .$this->keyname);
            error_log(print_r($cmd, true) . "\n\n", 3, '/srv/www/var/log/log_aws_down.txt');
            echo $cmd['Body'];
            exit();
        } else {
            echo "";
        }
        
    }
    
    
}


Output:
1
2

Parse error: syntax error, unexpected T_STRING on line 3


Create a new paste based on this one


Comments: