Narbeh
Narbeh

Reputation: 28

Get transferred file name in RabbitMQ using python pika

I'm using pika in Python3 to send CSV files from one node to another and everything is fine here. The only thing I need here is to get the transferred file name in the receiving node (e.g: filename-2017-01-01.csv)

Are there any ways to do this?

This is the processing part while getting the file.

def callback(ch, method, properties, body):
    ch.basic_ack(delivery_tag=method.delivery_tag)
    with open('test.csv', 'wb') as write_csv:
        write_csv.write(body)

Upvotes: 0

Views: 816

Answers (2)

Luke Bakken
Luke Bakken

Reputation: 9637

Instead of storing the file name with the message, you can use this method to add a custom header with the file name.

Upvotes: 2

Narbeh
Narbeh

Reputation: 28

I came up with this idea, for those who want to include the file name with the message:

I've concatenated the file name with the message. After sending the message, I did split on the message:

with open(file, 'rb') as csv_file:
    return file + csv_file.read().decode()

On the other side:

file_name = body.decode().split('.csv')[0]
message = body.decode().split('.csv')[1]
with open('{}.csv'.format(file_name), 'w') as write_csv:
    write_csv.write(message)

Upvotes: 0

Related Questions