agrodata / attachment
用于使用Agrodata附件服务的包
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.2
- illuminate/validation: ^9.19.0||^10.0||^11.0
This package is auto-updated.
Last update: 2024-09-09 14:19:40 UTC
README
<img src="https://img.shields.io/packagist/v/agrodata/attachment.svg" />
<img src="https://img.shields.io/packagist/dt/agrodata/attachment.svg" />
附件服务
此包负责在Laravel项目中配置和使用Agrodata的附件微服务。简而言之,该微服务负责在项目中处理所有S3数据的操作,以便将高处理需求(内存和处理器)集中在一点。此外,它还允许执行更重的操作,如同时下载多个.zip格式的文件以及上传大文件。
要了解更多信息,请访问微服务的文档
此包的作用是Laravel项目和微服务之间的“中间场”,因为它将使用项目的S3连接访问权限,发送到微服务,并接收结果;
安装
Laravel / Lumen
使用以下命令通过composer安装包
composer require agrodata/attachment-service
*仅适用于"Lumen框架"
在 `
bootstrap/app.php`
文件中注册provider文件,在文件末尾添加以下行。
$app->register(\Agrodata\AttachmentServiceProvider::class);
配置
要配置库,请使用以下命令,该命令将注册provider并在项目中创建配置文件(可选)。简而言之,配置文件仅查找项目中的Storage/S3数据,但必须提供包含附件微服务API URL的参数(AGRODATA_ATTACHMENT_URL)。
php artisan agrodata-attachment:install
<?php # config/agrodata-attachment.php
return [
'url' => env('AGRODATA_ATTACHMENT_URL'),
'key' => env('AWS_ACCESS_KEY_ID', config('filesystems.disks.s3.key')),
'secret' => env('AWS_SECRET_ACCESS_KEY', config('filesystems.disks.s3.secret')),
'region' => env('AWS_DEFAULT_REGION', config('filesystems.disks.s3.region')),
];
使用
配置后,可以在项目的任何地方实例化 \Agrodata\Attachment\AttachmentService::class 类,从而可以调用附件微服务的操作方法;
下载方法总是返回允许下载文件的链接。《code>`所有由服务生成的链接将有效期为5分钟`
downloadZipper(array $filenames): string
接收一个文件列表(必须在S3中存在,否则将生成错误),创建一个.zip格式的文件并返回下载链接。
返回zip文件的下载链接。
<?php
use Agrodata\Attachment\AttachmentService;
$downloadUrl = (new AttachmentService)->downloadZipper(["lista-compras.txt", "fornecedores.xlsx"]);
presignDownload(string $filename): string
接收一个文件名(必须存在于S3中)并返回下载链接。
<?php
use Agrodata\Attachment\AttachmentService;
$downloadUrl = (new AttachmentService)->presignDownload("lista-compras.txt");
queryDownload(string $filename, string $query): string
接收一个JSON数据源名称(必须存在于S3中)和一个查询。返回过滤后的JSON的“块”。
<?php
use Agrodata\Attachment\AttachmentService;
$jsonReturn = (new AttachmentService)->queryDownload("fonteDeDados.json", "select * from s3Object");
deleteAttachment(string $filename): string
接收一个文件名(必须存在于S3中)并执行删除。
<?php
use Agrodata\Attachment\AttachmentService;
$downloadUrl = (new AttachmentService)->deleteAttachment("lista-compras.txt");
presignUpload(string $filename): string
接收要保存在S3中的文件名,返回上传链接。对于接收到的此链接,应通过POST发送`file`参数,以实际完成附件的附加。这样,可以通过S3 API直接在前端进行上传,同时遵守bucket的限制和配置。
<?php
use Agrodata\Attachment\AttachmentService;
use Illuminate\Support\Facades\Http;
$uploadUrl = (new AttachmentService)->presignUpload("lista-compras.txt");
$fileSaved = Http::asForm()->put($uploadUrl, ['file' => ...binaryContent])->ok();
downloadPDFWatermarked(string $filename, string $text, array $options = []): string
接收一个保存在S3中的PDF文件名,要插入水印的文本以及一些额外的尺寸、颜色和角度选项。返回一个下载链接。方法为GET。
<?php
use Agrodata\Attachment\AttachmentService;
use Illuminate\Support\Facades\Http;
$downloadURL = (new AttachmentService)->downloadPDFWatermarked(
"relatorio.pdf", //filename
"Rascunho", //watermark text
[ //options
"size" => '150' //optional, default: 200
"angle" => '90', //optional, default: 90
"color" => 'lightgray' //optional, default: #CC00007F
]
);
附件使用完整流程