如何在php中生成s3 bucket后上传mpdf文件
生成后是否可以将mpdf文件上载到s3服务器如何在php中生成s3 bucket后上传mpdf文件,php,amazon-web-services,amazon-s3,mpdf,Php,Amazon Web Services,Amazon S3,Mpdf,生成后是否可以将mpdf文件上载到s3服务器 $file_name = $pdf->Output(time().'_'.'E-Prescription.pdf','F'); 要做到这一点,您可以使用 首先,您需要使用配置文件凭据创建一个客户端 use Aws\S3\S3Client; $client = S3Client::factory(array( 'credentials' => array( 'key' => 'YOUR
$file_name = $pdf->Output(time().'_'.'E-Prescription.pdf','F');
要做到这一点,您可以使用 首先,您需要使用配置文件凭据创建一个客户端
use Aws\S3\S3Client;
$client = S3Client::factory(array(
'credentials' => array(
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
)
));
并且,如果bucket已经存在,您可以像这样从文件系统上传您的文件:
$result = $client->putObject(array(
'Bucket' => $bucket,
'Key' => $file_name,
'SourceFile' => $pathToFile
));
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();
}
假设您使用composer在项目中安装了AWS SDK;具体地说
composer require aws/aws-sdk-php
是的,您可以这样使用流包装器:
$result = $client->putObject(array(
'Bucket' => $bucket,
'Key' => $file_name,
'SourceFile' => $pathToFile
));
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服务器):
然后编辑位于以下位置的ConfigVariables.php文件:
\vendor\mpdf\mpdf\src\Config
更改:
'tempDir' => __DIR__ . '/../../tmp',
致:
然后在同一目录中创建一个名为“tmp”的空文件夹。然后高兴地上传。//设置您的配置
// Set yours config's
define("AWS_S3_KEY", "2wsxXSW@3edcCDE#");
define("AWS_S3_SECRET", "1qazXSW@3edcVFR$5tgbNHYujm<KI*");
define("AWS_S3_REGION", "us-east-1");
define("AWS_S3_BUCKET", "files.uninotas.com.br");
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\U S3\U键”2wsxXSW@3edcCDE#");
定义(“AWS\U S3\U机密”1qazXSW@3edcVFR5tgbNHYujm美元