如何从php上传文件到amqp [英] how to upload a file from php to amqp

查看:90
本文介绍了如何从php上传文件到amqp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道...我如何从php发送文件到Rabbitmq队列. 我已经通过许多例子,其中大多数没有工作. 以下是一个即将开始运作的消费者生产者示例. 下面是一个Publisher.php

i want to know... how can i send a file to a rabbitmq queue from php. i have gone through many examples most of them didnt work. below is a consumer producer example which is near to working. Below is a the publisher.php

<?php
  require_once('../php-amqplib/amqp.inc');
    include('../config.php');
    $conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
    $channel = $conn->channel();
    $channel->exchange_declare('upload-pictures','direct', false, true, false);
    $metadata = json_encode(array(
    'image_id' => $argv[1],
    'user_id' => $argv[2],
    'image_path' => $argv[3]
    ));
    $msg = new AMQPMessage($metadata, array('content_type' => 'text/plain','delivery_mode'    => 2));
    $channel->basic_publish($msg, 'upload-pictures');
    $channel->close();
    $conn->close();
    ?>

consumer.php

consumer.php

<?php
require_once('../php-amqplib/amqp.inc');
include('../config.php');
$conn = new AMQPConnection(HOST, PORT, USER, PASS,VHOST);
$channel = $conn->channel();
$queue = 'add-points';
$consumer_tag = 'consumer';
$channel->exchange_declare('upload-pictures','direct', false, true, false);
$channel->queue_declare('add-points',false, true, false, false);
$channel->queue_bind('add-points', 'upload-pictures');
$consumer = function($msg){};
$channel->basic_consume($queue,$consumer_tag,false,false,false,false,$consumer);
?>

根据rabbitmq手册中提供的示例,我需要首先在一个终端中运行consumer(php Consumer.php),然后在另一个终端中运行发布者(php Publisher.php 1 2 file/path.png), 我将在消费者终端中收到向用户添加点数:2"的消息.我完全没有收到此消息.你能建议我哪里出问题了吗

according to the example provided in the rabbitmq manual, i need to run consumer(php consumer.php) first in one terminal and the publisher (php publisher.php 1 2 file/path.png) in another terminal, i will get a "Adding points to user: 2" message in the consumer terminal. iam not getting this message at all. can you suggest where iam going wrong

推荐答案

重要通知:

AMQP协议不是针对大型内容传输的(但是它可以完成这样的工作,我尝试了高达128Mb的测试). 我建议您在消息中发送文件的路径并在工作程序中处理它,而不是发送整个文件,同时非常快地存储大消息吃内存,并且当它超过磁盘时将使用它,这非常慢,与RAM相比.

AMQP protocol is not aimed for large content transfer (but it can do such job, I tried up to 128Mb for testing). I will suggest you to send path to file in message and process it in worker rather than sending whole file, while storing large message eat memory very fast and when it exceed disk will be used, which is pretty slow, comparing to RAM.

无论如何,如果您仍要发送文件:

您必须将文件内容作为常规邮件正文发送.如果它是二进制文件,则使用base64或任何您想要的文件对其进行编码.没有其他方法可以做您想要的.在您的代码中,我没有看到您正在读取文件.

You have to send file content as regular message body. If it is a binary file, then encode it with base64 or whatever you want. There is no other way to do what you want. In your code I didn't see that you are reading file.

这篇关于如何从php上传文件到amqp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆