bulkOperationRunMutation Upload JSONL file to https://shopify.s3.amazonaws.com

Solved
Yating
Tourist
4 0 0

Hi Guys, I have a question for the bulkOperationRunMutation Upload JSONL file to https://shopify.s3.amazonaws.com

this is my post-JSONL file to Shopify s3 code, 

Yating_0-1617810038700.png

I'm following the document https://shopify.dev/tutorials/bulk-import-data-with-admin-api

and trying to upload JSON lines file to the Shopify s3. but it doesn't let me. and gives me an error 

 

 {
  message: 'invalid json response body at https://shopify.s3.amazonaws.com/ reason: Unexpected token < in JSON at position 0',
  type: 'invalid-json'
}

 

for the stream is :

{"input":{"id":"gid://shopify/Product/6263822549185","descriptionHtml":"<p>the uptodated description11111111111</p>"}},

and for the key i got it right.

but still.

 

Please help me!

0 Likes
_JCC_
Shopify Staff
Shopify Staff
163 22 29

Hey @Yating ,

Happy to have a look at this for you.

I believe the response from AWS is XML. Is the error in await response.json()? As a test, await response.text() should confirm this.

Also noticing a minor difference between our tutorial and your implementation. In the tutorial, when uploading the file the example uses a cURL request, and notably is passing parameters as a form and not as headers. Have you tried building a FormData object adding the properties from the stagedUpload, append the file and sending the form as the request body

Hope this provides some help, if you have any other questions please don't hesitate to reach out.

Regards,

John

John C | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Yating
Tourist
4 0 0

Thank you so much, it really helps me.

but we used the FormData, and .text() for the error catching. it gives another error:

 

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>MalformedPOSTRequest</Code><Message>The body of your POST request is not well-formed multipart/form-data.</Message><RequestId>TEK3JK5J8MJR1NKB</RequestId><HostId>Dm+K+5eAi7moUhIe8LIZeqRoaStcnFNYsrs+9Ix3h+hlmAqIejxiVq95dVIP+JQM43Z/C/qGZ+s=</HostId></Error>

 

 

And the data form function looks like this: for the params we passed in (file path, and the authorization). 

 

 

function formData(path, data) {
const fs = require('fs');
const stream = fs.createReadStream(path);
const FormData = require('form-data');
let formData = new FormData();
for (let i = 0; i < data.length; i++) {
  //console.log(data[i]);
  formData.append(data[i].name, data[i].value);
}
formData.append("file", stream);
return formData;
}

 

and the Post function looks like this:
 

 

async function PostToShopify(path, data) {
    const response = await fetch(`https://shopify.s3.amazonaws.com/`, {
        method: 'POST',
        headers: { 'content-type': 'multipart/form-data' },
            body: data
    });
    result = await response.text();
    return result.data;
}

 

 

Can you please let me know where I go wrong?

0 Likes
_JCC_
Shopify Staff
Shopify Staff
163 22 29

This is an accepted solution.

Hey @Yating ,

In your most recently posted code I didn't see a call to your formData function. To try and help out, here's what I did when I first started experimenting with the bulk mutations.

/**
 * Upload jsonl file.
 * @param {object} paramStagedUpload - representing the parameters array returned from the staged upload call
 * @param {string} paramFilePath - path to jsonl file containing inputs for mutation
 */
const uploadFile = (paramStagedUpload, paramFilePath) => {
    const form = new FormData();
    paramStagedUpload.parameters.forEach((param) =>
        form.append(param.name, param.value)
    );

    form.append("file", fs.createReadStream(paramFilePath));

    return new Promise((resolve, reject) => {
        try {
            form.getLength((err, length) => {
                if(err)return reject(err);
                fetch(paramStagedUpload.url, {
                    method: "POST",
                    body: form,
                    headers: { "content-length": length },
                })
                    .then(function (res) {
                        return res.text();
                    })
                    .then(function (xml) {
                        parseXMLString(xml, function (err, result) {
                            if (result.Error) {
                                reject(new Error(result.Error.Message));
                            }
                            resolve(result.PostResponse.Location[0]);
                        });
                    });
            });
        } catch (err) {
            console.log(err);
            return reject(err);
        }
    });
};

 

John C | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Yating
Tourist
4 0 0

Thank you so much, really helps me

0 Likes
Avi_Ben_Zaken
Excursionist
14 0 5

Hey,

I have a JSON object that I'm creating in my server and need to upload to the File Page. How should this effect the code?

Is fs.createReadStream relevant in this case?

This code is not working for me, I'm getting 201 status, but the file is not uploaded.

At fileCreate mutation I'm getting status 200 but no 

files {
              alt
              createdAt
            }

Any suggestion here? thanks

0 Likes
Yating
Tourist
4 0 0

have you try to change the api version?

0 Likes