簡體   English   中英

php生成后如何上傳mpdf文件到s3 bucket

[英]How to upload mpdf file after generating to s3 bucket in php

生成后可以將mpdf文件上傳到s3服務器嗎?

$file_name = $pdf->Output(time().'_'.'E-Prescription.pdf','F');

假設您使用 composer 在您的項目中安裝了 AWS SDK; 具體來說...

composer require aws/aws-sdk-php

是的,您可以像這樣使用 stream 包裝器:

require "vendor/autoload.php";

$aws_file = 's3://bucketname/foldername/your_file_name.pdf';

//the folder is optional if you have one within your bucket

try {

    $s3->registerStreamWrapper();
    $mpdf->Output($aws_file, \Mpdf\Output\Destination::FILE);
}
catch (S3Exception $e) {

    $data['error'] = $e->getMessage();
    //show the error as a JSON callback that you can use for troubleshooting
    echo json_encode($data);
    exit();
}

您可能必須按如下方式向 web 服務器添加寫入權限(在 Ubuntu AWS EC2 上使用 Apache 服務器):

sudo chown -R www-data /var/www/html/vendor/mpdf/mpdf/src/Config/tmp
sudo chmod -R 755 /var/www/html/vendor/mpdf/mpdf/src/Config/tmp

然后編輯位於以下位置的 ConfigVariables.php 文件:

\vendor\mpdf\mpdf\src\Config

改變:

'tempDir' => __DIR__ . '/../../tmp',

到:

'tempDir' => __DIR__ . '/tmp',

然后在同一目錄中創建一個名為“tmp”的空文件夾。 然后愉快地上傳。

// Set yours config's
define("AWS_S3_KEY", "<your_key_here>");
define("AWS_S3_SECRET", "<your_secret_here>");
define("AWS_S3_REGION", "<your_region_here example:us-east-1>");
define("AWS_S3_BUCKET", "<your_bucket_folder_name_here>");

try {
    /*
    doc: https://github.com/mpdf/mpdf
    url/download: https://github.com/mpdf/mpdf/archive/development.zip
    */
    require_once 'mpdf/mpdf.php'; // load yout mdf libe
    $mpdf = new mPDF(); // set init object mPDF

    $nomeArquivo = md5('cliente_01'); // set file name and cripty this
    
    $mpdf->WriteHTML("Teste upload PDF in s3 bucket");

    /*
    doc: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/getting-started_installation.html
    url/download: https://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.zip
    */
    require_once 'aws/aws-autoloader.php'; // set locate yout lib AWS

    $aws_file = 's3://'.AWS_S3_BUCKET.'/'.$nomeArquivo.'.pdf';
    
    $s3 = new Aws\S3\S3Client([
        'region' => AWS_S3_REGION,
        'version' => 'latest',
        'credentials' => [
            'key'    => AWS_S3_KEY,
            'secret' => AWS_S3_SECRET,
        ]
    ]);

    $s3->registerStreamWrapper();
    $mpdf->Output($aws_file); //Send yout mPDF File in s3-file-bucket
} catch (S3Exception $e) {
    die($e->getError().' => '.$e->getMessage();
}

為此,您可以將AWS SDK 用於 PHP

首先,您需要使用您的個人資料憑據創建一個客戶端。

use Aws\S3\S3Client;

$client = S3Client::factory(array(
        'credentials' => array(
            'key'    => 'YOUR_AWS_ACCESS_KEY_ID',
            'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
        )
    ));

而且,如果存儲桶已經存在,您可以像這樣從文件系統上傳文件:

$result = $client->putObject(array(
    'Bucket'     => $bucket,
    'Key'        => $file_name,
    'SourceFile' => $pathToFile
));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM