Hello dear Pipedream Community!
I am trying for a while now to create files in Akeneo with Pipedream. It is also the first time for me to work with file streams at all.
I made some nice progress, but it seems like I can’t do the final leap without help.
The use case is, that I create a barcode in PDF_co and upload the created image to a dedicated media attribute in Akeneo.
What already works:
- Create a PNG of the barcode in PDF_co
- Download that image to TMP
- Read that file again from TMP
The steps are:
download_image_to_tmp
import stream from "stream";
import { promisify } from "util";
import fs from "fs";
import got from "got";
export default defineComponent({
async run({ steps, $ }) {
// Download the barcode image file to /tmp
const pipeline = promisify(stream.pipeline);
return await pipeline(
got.stream(steps.pdf_co_barcode_generator.$return_value.url),
fs.createWriteStream('/tmp/barcode.png')
);
}
})
read_file_from_tmp
import fs from "fs";
export default defineComponent({
async run({ steps, $ }) {
var myFile = fs.createReadStream('/tmp/barcode.png');
return myFile;
}
});
The documentation of Akeneo’s API endpoint is here and this is the Postman collection of Akeneo.
In Postman it works like this:
In Pipedream, I tried two different ways to upload the file.
Upload via HTTP POST Request step
I wanted to add a screenshot here, but I am allowed to upload only one.
Body
Content-Type: multipart/form-data
Name: product
Value: {“identifier”:“{{steps.trigger.event.body.events[0].data.resource.identifier}}”, “attribute”:“barcode”, “scope”: null,“locale”:null}
Name: file
Value: {{steps.read_file_from_tmp.$return_value}}
But I got the error:
source.on is not a function
As far as I could find out it seems to be related to the way the form is structured. But with this step, I can’t really influence that, right?
So I also tried to build the upload step myself with Axios, based on the code that was provided by Postman.
Upload with a Node step
import fs from "fs";
import { axios } from "@pipedream/platform"
import FormData from "form-data"
// To use previous step data, pass the `steps` object to the run() function
export default defineComponent({
async run({ steps, $ }) {
var data = new FormData();
data.append('product', '{"identifier":"' + steps.get_product_information.$return_value.identifier + '", "attribute":"barcode", "scope": null,"locale":null}');
data.append('file', await fs.createReadStream('/tmp/barcode.png'));
var config = {
method: 'post',
url: steps.akeneo.$return_value.host + '/api/rest/v1/media-files',
headers: {
'Authorization': 'Bearer '+ steps.akeneo.$return_value.token,
'Content-Type': 'multipart/form-data',
...data.getHeaders()
},
data : data
};
await axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
},
})
Here I get a timeout and a warning:
Code was still running when the step ended. Make sure to await all Promises, or promisify callback functions. May be a false positive
Has anyone some idea how to improve this? Preferably I would like to use the HTTP request step. So config in the UI wherever possible.
Thanks in advance!
Anton